Arrays类讲解

 

 1 package array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class ArrayDemo06 {
 6     public static void main(String[] args) {
 7         int[] a = {1,2,3,4,9090,31231,543,21,3,23};
 8         System.out.println(a);//[I@1b6d3586
 9         //打印数组元素Arrays.toString
10         System.out.println(Arrays.toString(a));
11 
12         Arrays.sort(a);//数组进行排序:升序
13         printArray(a);
14     }
15     public static void printArray(int[] a){
16         for (int i = 0; i < a.length; i++) {
17             if(i==0){
18                 System.out.print("[");
19             }
20             if (i==a.length-1){
21                 System.out.println(a[i]+"]");
22             }else{
23                 System.out.print(a[i]+", ");
24             }
25         }
26     }
27 }

 

 1 package array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class ArrayDemo06 {
 6     public static void main(String[] args) {
 7         int[] a = {1,2,3,4,9090,31231,543,21,3,23};
 8         System.out.println(a);//[I@1b6d3586
 9         //打印数组元素Arrays.toString
10         System.out.println(Arrays.toString(a));
11 
12         Arrays.sort(a);//数组进行排序:默认升序
13         System.out.println(Arrays.toString(a));
14         printArray(a);
15 
16         //数组填充:fill
17         Arrays.fill(a,0);//数组a用0填充
18         System.out.println(Arrays.toString(a));
19         Arrays.fill(a,2,4,100);//数组的2-4位用0填充
20         System.out.println(Arrays.toString(a));
21 
22     }
23     public static void printArray(int[] a){
24         for (int i = 0; i < a.length; i++) {
25             if(i==0){//第一个
26                 System.out.print("[");
27             }
28             if (i==a.length-1){//最后一个
29                 System.out.println(a[i]+"]");
30             }else{//其余
31                 System.out.print(a[i]+", ");
32             }
33         }
34     }
35 }

 

posted on 2022-07-29 17:16  一枚努力学习的小白  阅读(18)  评论(0编辑  收藏  举报