JAVA学习部分---数组

目录

1.找出数组里的最大值,并显示下标

2.数字里插入一个数并保证升序

3.二维数组经典问题--杨辉三角

4.随机产生1-10的数字组成一个长度为10的数组,保证不重复

5.方法和数组的巧妙结合(学生成绩输入系统)


数组使用注意事项和细节

1) 数组是多个相同类型数据的组合,实现对这些数据的统一管理

2) 数组中的元素可以是任何数据类型,包括基本类型和引用类型,但是不能混用。

3) 数组创建后,如果没有赋值,有默认值

int 0short 0, byte 0, long 0, float 0.0,double 0.0char \u0000boolean falseString null

4) 使用数组的步骤 1. 声明数组并开辟空间 2 给数组各个元素赋值 3 使用数组

5) 数组的下标是从 0 开始的

6) 数组下标必须在指定范围内使用,否则报:下标越界异常,比如 int [] arr=new int[5]; 则有效下标为 0-4

7) 数组属引用类型,数组型数据是对象(object)

1.找出数组里的最大值,并显示下标

    int[] scores = {4,-1,9,10,23};
	int max = scores[0];
	int maxIndex = 0;

	for(int i = 1 ;i<scores.length;i++){
		if(max<scores[i]){
			max = scores[i];
			maxIndex = i;
		}
	}
	System.out.println("最大值为:"+max);
	System.out.println("最大值对应的下标为:"+maxIndex);

2.数字里插入一个数并保证升序

        int num = 23;
		int index=-1;
		int[] arr ={10,12,45,90};
		int[] arr2=new int[arr.length+1];
		for(int i = 0;i<arr.length;i++){
			if(arr[i]>num){
				index = i;
				break;
			}
		}
		if(index == -1){
			index = arr.length;
		}
		for(int i=0,j=0;i<arr2.length;i++){
			if(i!=index){
				arr2[i]=arr[j];
				j++;
			}else{
				arr2[i]=num;
			}
		}
		arr = arr2;
		System.out.println("===排序后===");
			for(int i = 0;i<arr.length;i++){
				System.out.print(arr[i]+"\t");
			}

3.二维数组经典问题--杨辉三角

        int[][] arr = new int[10][];

		for(int i =0;i<arr.length;i++){
			//给每个一维数组(行)开空间
			arr[i] = new int[i+1];

			//给每个一维数组(行)赋值
			for(int j = 0;j<arr[i].length;j++){
				 //每一行的第一个元素和最后一个元素都是1
				 if(j==0||j==arr[i].length-1){
				 	arr[i][j]=1;
				 }else{
				 	arr[i][j]= arr[i-1][j] + arr[i-1][j-1];
				 }
			}
		}
		for(int i = 0;i<arr.length;i++){
			for(int j =0;j<arr[i].length;j++){
				System.out.print(arr[i][j]+"  ");
			}
			System.out.println();	
		}

4.随机产生1-10的数字组成一个长度为10的数组,保证不重复

static Random ran = new Random();
	static int SIZE = 10;
	static int index = 0;
	static int target = 0;
	static int[] a = new int[SIZE];

	public static void main(String[] args) {
		long s1 = System.nanoTime();
		while (index < SIZE) {
			int t = ran.nextInt(10) + 1;
			boolean f = hasNum2(t);
			if (!f) {
				a[index++] = t;
			}
		}
		long s2 = System.nanoTime();
		System.out.println("耗时" + (s2 - s1));
		print();

	}

	public static boolean hasNum2(int t) {// bitmap
		int shift = 1; // 00000000 00000000 00000000 00000001
		shift = shift << t; // 00000000 00000000 00000000 00000100

		if ((shift & target) == 0) {
			target = target | shift;
			return false;
		} else {
			return true;
		}
	}

	public static void print() {
		for (int t : a) {
			System.out.print(t + " ");
		}
	}

总结:在调用hasNum2的里,运用的bitmap的编程思想,即位运算符,即可快速实现产生的随机数是否重复,大大缩短了运行时间。

在调用print方法时,运用了for...each循环可以缩减代码。

5.方法和数组的巧妙结合(学生成绩输入系统)

static Scanner sc = new Scanner(System.in);
	static String[][] students = new String[3][4];
	static int index = 0;
	static int total = 0;

	public static void main(String[] args) {
		add();
		add();
		add();
		add();
		add();
		print();
		System.out.println();
		System.out.println(scoreTotal());
		System.out.println(avg());
		System.out.println(passNum());
	}

	public static int scoreTotal() {

		for (int i = 0; i < index; i++) {
			total += Integer.parseInt(students[i][3]);
		}
		return total;

	}

	public static double avg() {
		return 1.0 * total / index;
	}

	public static int passNum() {
		int num = 0;

		for (int i = 0; i < index; i++) {
			int score = Integer.parseInt(students[i][3]);
			if (score >= 60) {
				num++;
			}

		}
		return num;

	}

	public static void add() {
		if (index >= students.length) {// 数组扩容
			int newLength = students.length + (students.length >> 1);
			String[][] ss = new String[newLength][4];

			System.arraycopy(students, 0, ss, 0, students.length);
			students = ss;
		}
		/**
		 * 信息输入
		 */
		System.out.print("请输入学生姓名:");
		String name = sc.nextLine();

		System.out.print("请输入学生科目:");
		String subject = sc.nextLine();

		System.out.print("请输入学生成绩:");
		String score = sc.nextLine();

		String studNum = "dyit" + System.currentTimeMillis();

		String[] stu = { studNum, name, subject, score };

		students[index++] = stu;
	}

	public static void print() {
		System.out.println(Arrays.deepToString(students));
	}

总结:在进行数组扩容时(add方法),可直接调用System.arraycopy(老数组名,从哪个下标开始,新数组名,从哪个下标开始,新数组的长度)来存储数据,非常方便。

在进行二维数组遍历时(print方法),可以调用Arrays.deepToString(数组名),遍历一维数组时,可采用Arrays.toString()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值