Arrays类

Arrays类

 

```java
package com.zishi.Array;

import java.util.Arrays;

public class ArrayDemo06 {
public static void main(String[] args) {
int[] a = {1,2,5,6,2,5,6,25,2,6,22,565};

System.out.println(a); //[I@1b6d3586 哈希

//打印数组元素Arrays.toString
System.out.println(Arrays.toString(a));

 

Arrays.sort(a); //对数组进行排序:升序排列
System.out.println(Arrays.toString(a));

Arrays.fill(a,0);//填充
System.out.println(Arrays.toString(a));

Arrays.fill(a,2,4,0); //2到4之间填充
System.out.println(Arrays.toString(a));

System.out.println("=========================");
printArray(a);
}

//自己写一个Arrays.toString

public static void printArray(int[] a){
for (int i = 0; i < a.length; i++) {
if(i == 0){
System.out.print("[");
}
if(i == a.length-1){
System.out.print(a[i]+"]");
}else
System.out.print(a[i]+", ");
}
}
}
```

 

posted @ 2021-07-23 01:04  子时未临  阅读(20)  评论(0编辑  收藏  举报