JAVA07-数组

1、遍历数组

  * 通过for循环可以遍历数组

public class Main {
    public static void main(String[] args) {
        int[] ns = { 1, 4, 9, 16, 25 };
        for (int i=0; i<ns.length-1; i++) {
            System.out.print(ns[i]+" ");
        }
    }
}

  

  * 通过for each循环,直接迭代数组的每个元素;

1 public class Main {
2     public static void main(String[] args) {
3         int[] nums= {1,2,3,4,5,6,7,8,9};
4         for(int i:nums) {
5             System.out.print(i+" ");
6         }
7     }
8 }

2、打印数组内容

  * 使用for each循环打印数组的元素内容;

  * 使用标准库提供的Arrays.toString(),可以快速打印数组内容;

1 import java.util.Arrays;
2 
3 public class Main {
4     public static void main(String[] args) {
5         int[] ns = { 1, 1, 2, 3, 5, 8 };
6         System.out.println(Arrays.toString(ns));
7     }
8 }

3、数组排序

  * 冒泡排序:每一轮循环后,最大的一个数被交换到末尾,因此,下一轮循环就可以去除最后的数,每一轮循环都比上一轮循环的结束位置靠前一位。

 1 import java.util.Arrays;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
 6         // 排序前:
 7         System.out.println(Arrays.toString(ns));
 8         for (int i = 0; i < ns.length - 1; i++) {
 9             for (int j = 0; j < ns.length - i - 1; j++) {
10                 if (ns[j] > ns[j+1]) {
11                     // 交换ns[j]和ns[j+1]:
12                     int tmp = ns[j];
13                     ns[j] = ns[j+1];
14                     ns[j+1] = tmp;
15                 }
16             }
17         }
18         // 排序后:
19         System.out.println(Arrays.toString(ns));
20     }
21 }

  

  * 使用java内置的排序功能,Arrays.sort()

1 import java.util.Arrays;
2 
3 public class Main {
4     public static void main(String[] args) {
5         int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
6         Arrays.sort(ns);
7         System.out.println(Arrays.toString(ns));
8     }
9 }

  

4、二维数组:就是数组的数组,对每个数组的长度无要求,可以将这个几维看作几个数组嵌套,访问数组元素可以依次找嵌套内容

 1 public class Main {
 2     public static void main(String[] args) {
 3         int[][] ns = {
 4             { 1, 2, 3, 4 },
 5             { 5, 6, 7, 8 },
 6             { 9, 10, 11, 12 }
 7         };
 8         System.out.println(ns.length); // 3,指3个数组
 9     }
10 }
 1 public class Main {
 2     public static void main(String[] args) {
 3         int[][] ns = {
 4             { 1, 2, 3, 4 },
 5             { 5, 6, 7, 8 },
 6             { 9, 10, 11, 12 }
 7         };
 8         int[] arr0 = ns[0];  //定义一个新的数组,将ns[0]赋值给他
 9         System.out.println(arr0.length); // 4,指ns[0]中的元素个数
10     }
11 }

  

  访问二维数组的某个元素使用array[row][col]。

 1 public class Main {
 2     public static void main(String[] args) {
 3         int[][] ns = {
 4             { 1, 2, 3, 4 },
 5             { 5, 6, 7, 8 },
 6             { 9, 10, 11, 12 }
 7         };
 8         int[] arr0 = ns[0];
 9         System.out.println(ns[1][2]); // 7,第一个数组(索引为1)中的第二个元素
10     }
11 }

  打印一个二维数组,可以使用两层嵌套的for循环 / 使用Arrays.deepToString()

 1 public class Main {
 2     public static void main(String[] args) {
 3         int[][] ns= {
 4                 {1,2,3,4},
 5                 {5,6,7,8},
 6                 {9,10,11,12}
 7         };
 8         System.out.print(ns.length+"
");
 9         System.out.print(ns[1][2]+"
");
10 //打印二维数组,方法一
11         for(int[] arrs:ns) {
12             for(int arr:arrs) {
13                 System.out.print(arr);
14                 System.out.print(',');
15             }
16             System.out.println();
17         }
18 //打印二维数组,方法二
19         System.out.print(Arrays.deepToString(ns));
20     }
21 }
 1 public class Main {
 2     public static void main(String[] args) {
 3         //使用二维数组表示一组学生的各科成绩,计算所有学生的平均分
 4         int[][] scores = {
 5                 { 82, 90, 91 },
 6                 { 68, 72, 64 },
 7                 { 95, 91, 89 },
 8                 { 67, 52, 60 },
 9                 { 79, 81, 85 },
10         };
11         double average = 0;
12         int sum=0,count=0;
13         for(int[] arrs:scores) {
14             for(int arr:arrs) {
15                 sum+=arr;
16                 count+=1;
17             }
18             
19         }
20         average=sum/count;
21         System.out.println(average);
22     }
23 }

5、命令行参数

java入口main方法,接收一个命令行参数,是个String[] 数组。是由JVM接受用户输入并传给main方法。

 1 public class Main {
 2     public static void main(String[] args) {
 3         for (String arg : args) {
 4             if ("-version".equals(arg)) {
 5                 System.out.println("v 1.0");
 6                 break;
 7             }
 8         }
 9     }
10 }
原文地址:https://www.cnblogs.com/Free-Ink/p/12649760.html