数组
int[] nums;//数组声明
nums = new int[10];//创建一个数组
int sum = 0;
nums [0] = 1;
nums [1] = 2;
nums [2] = 3;
nums [3] = 4;
nums [4] = 5;
nums [5] = 6;
nums [6] = 7;
nums [7] = 8;
nums [8] = 9;
nums [9] = 10;
for(int i = 0;i< nums.length ;i++){
sum = sum + nums[i];
}
System.out.println(sum);
int[] nums ={1,2,3,4,5};
//输出
for(int i=0 ;i< nums.length ;i++){
System.out.println(nums[i]);
}
System.out.println("=============================");
//计算平均数
int sum=0;
for(int i=0 ;i< nums.length ;i++){
sum += nums[i];
}
System.out.println( (sum/nums.length));
System.out.println("=============================");
//最大值
int max = nums[0];
for(int i=1 ;i< nums.length ;i++){
if(nums[i]>max){
max = nums[i];
}
}
System.out.println(max);
public static void main(String[] args) {
int[] nums ={1,2,3,4,5};
int[] reverse= reverse(nums);
printArrays(reverse);
}
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
for(int i=0 ,j= arrays.length-1;i<arrays.length;i++,j--){
result[i]=arrays[j];
}
return result;
}
public static void printArrays(int[] arrays){
for (int i=0;i<arrays.length ;i++){
System.out.print(arrays[i]+" ");
}
}
public static void main(String[] args) {
int[][] nums ={{1,3},{2,4},{3,6},{4,8},{5,10}};
printArrays(nums[0]);//输出为1,3
//输出全部元素
for(int i =0;i<nums.length;i++){//为外部长度
for(int j = 0;j< nums[0].length;j++){//为每个的长度
System.out.println(nums[i][j]);
}
}
}
public static void printArrays(int[] arrays){
for (int i=0;i<arrays.length ;i++){
System.out.print(arrays[i]+" ");
}
}
int[][] array1= new int[11][11];
array1[1][2]=1;
array1[2][3]=2;
int[][] array2 = new int[3][3];
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=3;
int count=0;
for(int i=0; i<array1.length;i++){
for(int j=0;j<array1[i].length;j++){
if(array1[i][j]!=0){
count++;
array2[count][0]=i;
array2[count][1]=j;
array2[count][2]=array1[i][j];
}
}
}
for(int i=0;i< array2.length;i++){
System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]);
}
int[][] array3=new int[array2[0][0]][array2[0][1]];
for(int i = 1;i< array2.length;i++){
array3[array2[i][0]][array2[i][1]]=array2[i][2];
}
for(int i=0;i<array3.length;i++){
for(int j = 0;j<array3[i].length;j++){
System.out.print(array3[i][j]+" ");
}
System.out.println();
}
标签:java,nums,int,sum,System,length,数组,out
From: https://www.cnblogs.com/the-computer-journey-of-shuyu/p/18688779