栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA学习部分---数组

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA学习部分---数组

目录

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

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

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

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

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


数组使用注意事项和细节
1) 数组是多个相同类型数据的组合,实现对这些数据的统一管理 2) 数组中的元素可以是任何数据类型,包括基本类型和引用类型,但是不能混用。 3) 数组创建后,如果没有赋值,有默认值 int 0,short 0, byte 0, long 0, float 0.0,double 0.0,char u0000,boolean false,String 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 

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

        int num = 23;
		int index=-1;
		int[] arr ={10,12,45,90};
		int[] arr2=new int[arr.length+1];
		for(int i = 0;inum){
				index = i;
				break;
			}
		}
		if(index == -1){
			index = arr.length;
		}
		for(int i=0,j=0;i

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

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

		for(int i =0;i

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()。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号