数组的打印(3种)及简单调用

数组的定义及打印

 1 import java.util.Arrays;
 2 
 3 public class test7 {
 4 
 5     /**
 6      *
 7      * @param args
 8      */
 9     public static void main(String[] args) {
10         int[] array1 = {1,2,3};
11         int[] array2 = new int[3];//定义array2的长度
12         int[] array3 = new int[]{1,2,3,4};
13         //数组遍历
14         for (int i = 0; i < array1.length; i++) {
15 
16             System.out.print(array1[i]);
17         }
18         System.out.println();
19         System.out.println("=======foreach=======");
20         for (int i:array3) {//i为值,array是数组
21             System.out.print(i);
22         }
23         System.out.println();
24         System.out.println("=====数组内容以字符串的形式输出=====");
25         System.out.println(Arrays.toString(array1));
26     }
27 }

调用数组

 1 public class test7 {
 2 
 3     /**
 4      *
 5      * @param arr
 6      * 
 7      */
 8     public static void fac(int[] arr) {
 9         for (int i = 0; i < arr.length ; i++) {
10             System.out.print(arr[i] + " ");
11         }
12     }
13     public static void main(String[] args) {
14         int[] arr = {1,2,3};
15        fac(arr);
16     }
17 }

猜你喜欢

转载自www.cnblogs.com/Leafbud/p/12811063.html