import java.util.Arrays; public class Copy2 { public static void main(String [] args){ //数组填充 fill(要元素替换的数组int[]a,填充的值int value) int []a = new int [6]; Arrays.fill(a,8); //将a数组中所有元素填为8 for(int n:a){ //foreach遍历数组 System.out.print(n+" "); } System.out.println(); //指定填充的范围 fill(int[]a,起始int fromIndex,终止int toIndex,int value) Arrays.fill(a,1,5,0); //指定把下标为1-5的元素替换为0(不包括5) for(int n:a){ System.out.print(n+" "); } System.out.println(); System.out.println(); System.out.println("数组排序"); //数组排序 sort(要排序的数组) int b[]={23,45,12,65,2,11,0,45}; //定义数组 for(int n:b){ System.out.print(n+" "); } System.out.println(); Arrays.sort(b); //调用排序方法 for(int n:b){ System.out.print(n+" "); } System.out.println(); System.out.println(); System.out.println("数组的复制:"); //数组的复制 int [] newb=Arrays.copyOf(b,5); //当b.length>newb.length时,将b数组的前5个元素复制到newb数组 for(int n:newb){ System.out.print(n+" "); } System.out.println(); int [] newb1 = Arrays.copyOf(b, 14); //当b.length<newb1.length时,空余的元素位用0填充 for(int n:newb1){ System.out.print(n+" "); } System.out.println(); int [] newb2 = Arrays.copyOfRange(b, 2, 7); //将b数组下标为2-7的元素复制到newb2数组中 for(int n:newb2){ System.out.print(n+" "); } System.out.println(); System.out.println(); } }
时间: 2024-10-18 16:25:02