数组总结

数组总结~

1.数组定义
**要点

连续空间
类型确定
空间固定大小
**不初始化,默认值为:原始类型0,引用类型null
2.定义方式4种
类型[] 数组名 = new 类型[];

类型[] 数组名 = new 类型[]{值,值。。。。};

**注意

类型[] 数组名 = new 类型[];

数组名 = {值,值。。。}

是错误的~数组名内存的是数组的首地址

3练习

第一个练习是要用一种方法实现一位数组的排序,很简单,略过。第二个是要实现随机产生一个二维数组,并且找到最大数,输出其所在行和列,以及出现次数

package shuzu;

import java.util.Random;

public class paixu2 {

	/**
	 * 练习二
	 */
	public static void main(String[] args) {
		int[][] x = new int[3][3];
		System.out.println("初始数组为:\n");
		for(int i=0;i<3;i++){
			for(int j = 0;j<3;j++){
			Random rand = new Random();
			x[i][j] = rand.nextInt(100);
			System.out.print(x[i][j]+" ");
			}
			System.out.print("\n");
		}
		CHARU(x);
		

	}
	public static void CHARU(int[][] x){
		//用一个一位数组记录每一行的最大值
		int[] maxx = {0,0,0};
		//用一个一位数组记录每一行最大值所在的列
		int[] maxy = {0,0,0};
		//用Int变量记录最大值
		
		for(int i = 0;i<3;i++){
			   for(int j =0 ;j<3;j++){
				  if(maxx[i]<x[i][j]){
					  maxx[i] = x[i][j];
					  maxy[i] = j+1;
				  }
					  
				  }
	     
					  
				  
				   
			   }
		int max = maxx[0];
		//用于记录最大值的行数
		int I = 1;
		for(int i= 1;i<3;i++){
			if(maxx[i]>max){
				max = maxx[i];
				I = i+1;
			}
			
		}	
		//计算最大值出现的次数
		int count = 0;
		for(int i= 0;i<3;i++){
			if(maxx[i]==max){
				count++;
			}
			
		}	
		
		System.out.println("最大的数字为第"+I+"行,第"+maxy[I]+"列的"+max+",共有"+count+"个");
		   
		   
	   }

	}

 代码有很多地方的实现不够巧妙,绕了很多远路,求大神指教~~