数组处理
2021-04-12 06:28
标签:数组 tostring auth 一个 fill 复制 覆盖 main 打印 数组处理 标签:数组 tostring auth 一个 fill 复制 覆盖 main 打印 原文地址:https://www.cnblogs.com/fuqiang-java/p/13356369.htmlimport java.util.Arrays;
/**
* @auther 付强
* @date 2020/7/21 - 13:46
*/
public class java6 {
public static void main(String[] args) {
//一维数组
int[] arr=new int[]{1,2,3,5,6};//还可以几种表现形式
int []arr3=new int[5];
arr3[0]=11;
arr3[1]=10;//利用new分配内存时里面的元素初始值都是0;
int arr2[]={1,5,3,5,8,5};
String str=new String("12345");
char[] arr1={‘q‘,‘s‘,‘d‘};
String str1=new String(arr1);
System.out.println(str);
System.out.println(str1);
int day[]=new int[]{31,28,30,40};
for (int i = 0; i System.out.println(day[i]);
}
//二维数组
int []arr4[]=new int[3][4];
// 必须前边加一个,即必须行必须有数,否则报错,初始化后的值是0
int [][]arr6=new int[6][4];
for (int i = 0; i for (int j = 0; j System.out.print(arr6[i][j]);
}
System.out.println();//加入空格
}
int arr5[][]={{1,2,3},{54,9,5}};
for (int i = 0; i for (int j = 0; j System.out.print(arr4[i][j]);
}
System.out.println();
}
for (int i = 0; i for (int j = 0; j System.out.print(arr5[i][j]+",");
}
System.out.println();
}
int arr7[][]={{1},{2,3},{4,5,6}};
for (int i = 0; i for (int j = 0; j System.out.print(arr7[i][j]);
}
System.out.println();
}
int arr8[][]={{1,2},{3,4}};
//进行打印1
System.out.println("这个数组里面所包含的元素都有:");
loop:
for (int i = 0; i for (int j = 0; j System.out.print(arr8[i][j]);
if(i==arr8.length-1&&j==arr8[0].length-1){
break loop;
}else{
System.out.print(",");
}
}
}
// 进行打印2
System.out.println();
int i=0;
for(int arr9[]:arr8){
int j=0;
for(int arr10:arr9){
if(i==arr8.length-1&&j==arr9.length-1){
System.out.print(arr8[i][j]);
}else{
System.out.print(arr8[i][j]+",");
}
j++;
}
i++;
}
System.out.println();
// 数组元素覆盖,将一维数组全部覆盖为8
int arr11[]=new int[8];
Arrays.fill(arr11,8);
System.out.println(Arrays.toString(arr11));
Arrays.fill(arr11,1,5,6);
// 怎么说呢,将索引1到索引5的变成6,也就是说包头不包尾
System.out.println(Arrays.toString(arr11));
int arr12[]={1,8,3,58,6,58,9,6};
Arrays.fill(arr12,2,6,12);
System.out.println(Arrays.toString(arr12));
Arrays.sort(arr12);
// 进行排序,然后求出结果
System.out.println(Arrays.toString(arr12));
// 长度如果大于总长度的话会用0顶替,小于的话会被会从头截取,截取到这个长度结束
// 利用这种方式可以产生一个新的数组,利用复制的方式来获得新的数组
int[] arr16 = Arrays.copyOf(arr, 4);
}
}