一维数组:
静态初始化:
定义格式:(1)数据类型[] 数组名 = new 数组类型[]
(2)数组类型[] 数组名 ={元素1,元素2,.....}
练习:数组元素逆序:
public static void main(String[] args) {
int[] arr ={1,2,3,4,5,6,7,8,9,10};
System.out.println("逆序前:");
for(int i = 0;i< arr.length;i++){
if (i==0){
System.out.print("["+arr[0]+",");
} else if (i== arr.length-1) {
System.out.print(arr[i]+"]");
}else {
System.out.print(arr[i]+",");
}
}
for(int font = 0,end = arr.length-1;font<end;font++,end--){
int tmp= arr[font];
arr[font] = arr[end];
arr[end] = tmp;
}
System.out.println("逆序后:");
for(int i = 0;i< arr.length;i++){
if (i==0){
System.out.print("["+arr[0]+",");
} else if (i== arr.length-1) {
System.out.print(arr[i]+"]");
}else {
System.out.print(arr[i]+",");
}
}
}
二维数组
定义格式:(1)数据类型[][] 数组名 = new 数据类型[m][n]
(2)数据类型[][] 数组名 = new 数据类型[m][]
(3)数据类型[][] 数组名 = {{},{},{},........}
练习:杨辉三角
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要输出的行:");
int row=sc.nextInt();
int[][] arr=new int[row][];
for(int i=0;i<row;i++){
arr[i] = new int[i+1];
arr[i][0]=1;
arr[i][i]=1;
for(int j=1;j<i;j++){
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
}
for(int x=0;x<arr.length;x++){
for (int y=0;y<=x;y++){
System.out.print(arr[x][y]+"\t");
}
System.out.println();
}
}
标签:初始化,arr,Java,int,数据类型,System,笔记,print,out From: https://www.cnblogs.com/black-rosebush/p/17787470.html