数组玩法(二)
2016-07-30 00:32
246 查看
本博客主要是以引用型数组为例子,介绍各种玩法。
各种玩法本质和基本数据类型一致,不做详细介绍了。
核心:引用型数组,操作的是引用,而不是对象本身,各种操作都是地址的事。
先来一个Student类
开始玩:
简单粗暴,直接贴一个大型代码块,满足所有需求。
详情参考数组玩法(一)
留言一起学习,一起进步。
各种玩法本质和基本数据类型一致,不做详细介绍了。
核心:引用型数组,操作的是引用,而不是对象本身,各种操作都是地址的事。
先来一个Student类
/** 写一个学生类,属性:int id, String name, int grade, double score 将该类属性进行封装,为外界提供公共的get/set方法 无参构造器,全参构造器 声明一个say()方法,返回对象的所有属性 */ public class Student { //属性 private int id; private String name; private int grade; private double score; //无参构造器 public Student() { } //全参构造器 public Student(int id, String name, int grade, double score) { this.id = id; this.name = name; this.grade = grade; this.score = score; } //id的set/get方法 public void setId(int id) { this.id = id; } public int getId() { return id; } //setName的set/get方法 public void setName(String name) { this.name = name; } public String getName() { return name; } //setGrade的set/get方法 public void setGrade(int grade) { this.grade = grade; } public int getGrade() { return grade; } //setScore的set/get方法 public void setScore(double score) { this.score = score; } public double getScore() { return score; } //say()方法,返回对象的所有属性 public String say() { return "学号:" + id + "\t姓名:" + name + "\t年级:" + grade + "\t分数:" + score; } }
开始玩:
/** 写一个测试类,测试引用类型数组的各种玩法 1) 创建一个学生对象数组有20个元素, 使用循环创建对象, 学生的id是从1-20,姓名, 年级和分数都是随机生成 遍历这个数组,打印每个对象的信息 2) 遍历数组时, 只打印3年级的学生的信息 3) 求学生的平均分数.. 4) 找出学生中的最高分和最低分 //程序的编写要提高程序的灵活性 */ public class StudentTest { public static void main(String[] args) { //设置姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; //姓 String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //名 //创建一个元素类型是Student的,数组名是stuArr,长度为20的数组 Student[] stuArr = new Student[20]; /* 设置循环给stuArr数组赋值 其中id 1~20 name 随机生成(姓名库) grade随机生成[1~6] score随机生成[0~100] */ for (int i = 0; i < stuArr.length; i++) { int id = i + 1; //随机生成姓 名的索引 int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); //合成随机姓名 String name = name1[randIndex1] + name2[randIndex2]; //生成随机年级 int grade = (int)(Math.random() * 6 + 1); //生成随机分数 double score = (int)(Math.random() * 101); //int id, String name, int grade, double score //根据生成的随机的数据给Student型数组赋值 stuArr[i] = new Student(id, name, grade, score); } /* //经典for循环 for (int i = 0; i < stuArr.length; i++) { System.out.println(stuArr[i].say()); } */ /* 增强for循环,只能遍历数组, 当在遍历的时候需要做一些其他的事时只能使用经典for循环 语法: 元素类型 变量名 : 数组名 {sys(变量名)} */ for(Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("----------------------------------"); // 2) 遍历数组时, 只打印3年级的学生的信息 for (int i = 0; i < stuArr.length; i++) { //根据条件打印学生的信息 if (stuArr[i].getGrade() == 3) { System.out.println(stuArr[i].say()); } } System.out.println("----------------------------------"); // 3) 求学生的平均分数 //定义一个保存成绩的变量sum double sum = 0; //利用for循环遍历,并求和 for (int i = 0; i < stuArr.length; i++) { //求和 sum += stuArr[i].getScore(); } //计算平均分 //能用变量的地方不使用字面值,提高程序的灵活性 double avg = sum / stuArr.length; //打印平均分 System.out.println("平均分:" + avg); System.out.println("-----------------------------------"); // 4)通过值找出学生中的最高分和最低分(只找出最高分和最低分就行) //将数组中的第一个元素的分数赋值给max double max = stuArr[0].getScore(); //将数组中的第一个元素的分数赋值给min double min = max; //遍历数组 for (int i = 0; i < stuArr.length; i++) { //如果第i个元素的分数大于max,修改max的值 if (stuArr[i].getScore() > max) { max = stuArr[i].getScore(); } //如果第i个元素的分数小于min,修改min的值 if (stuArr[i].getScore() < min) { min = stuArr[i].getScore(); } } //打印 System.out.println("最高分:" + max + ",最低分:" + min); System.out.println("----------------------------------"); //找出最低分的学生(要可以找到这个学生对象,而不仅仅找到最低分) //时刻牢记,操作对象实际是在操作对象的引用 /* 因为数组元素的类型是Student型的,实际上,每个数组元素相当于 一个Student型的引用变量,它们其中保存了它们所指向的对象的地址 */ //创建一个保存第一个数组元素的Student型的引用变量 Student minScoreStudent = stuArr[0]; //循环遍历 for(int i = 0; i < stuArr.length; i++) { //将第i数组元素所指向的对象的分数与minScoreStudent所指向的对象的分数进行比较, //若小于,修改minScoreStudent if(stuArr[i].getScore() < minScoreStudent.getScore()) { minScoreStudent = stuArr[i]; } } System.out.println("最低分的学生是:" + minScoreStudent.say()); System.out.println("----------------------------------"); //找出最高分的学生(要可以找到这个学生对象,而不仅仅找到最高分) //最高分和最低分原理一致 Student maxScoreStudent = stuArr[0]; //for循环 for(int i = 0; i < stuArr.length; i++) { //判断 if(stuArr[i].getScore() > maxScoreStudent.getScore()) { //修改 maxScoreStudent = stuArr[i]; } } System.out.println("最高分的学生是:" + maxScoreStudent.say()); } } /** 利用上边随机创建的Student数组,进行一些数组的常用练习 通过索引找,最高分的学生 */ class StudentTest2 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[20]; //循环遍历给数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } //通过索引找,最高分的学生 int stuMaxIndex = 0; //for循环遍历 for(int i = 0 ; i < stuArr.length; i++) { if(stuArr[i].getScore() > stuArr[stuMaxIndex].getScore()) { stuMaxIndex = i; } } System.out.println("最高分的学生是:" + stuArr[stuMaxIndex].say() + "下标为" + stuMaxIndex); } } /** 找出三年级的最高分和最低分,求平均分 不知道第一个三年级的学生的分数是多少或者值是多少 这种情况我们一开始并不知道max/min应该赋什么值,或者用什么索引 只能给max/min赋极值来解决问题 max问题 先给max赋一个最小的值(所有情况都比它大) min问题 先给min赋一个最大的值(所有情况都比它小) */ class StudentTest3 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[5]; //for循环遍历给数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } //找出三年级的最高分和最低分,求平均分 double maxScore = -1; double minScore = 101; double sumScore = 0; int count = 0; for(int i = 0; i < stuArr.length; i++) { //首先只有是三年级的学生才符合我们的要求 if(stuArr[i].getGrade() == 3) { //判断最大值 if(stuArr[i].getScore() > maxScore){ maxScore = stuArr[i].getScore(); } //判断最小值 if(stuArr[i].getScore() < minScore){ minScore = stuArr[i].getScore(); } //对分数进行求和 sumScore += stuArr[i].getScore(); //计数 count ++; } } //输出的时候要考虑一个三年级的学生也没有怎么办 if(count == 0) { System.out.println("没有三年级的学生"); } else{ double avg = sumScore / count; System.out.println("三年级中的最高分是:" + maxScore); System.out.println("三年级中的最低分是:" + minScore); System.out.println("三年级中的平均分是:" + avg); } } } /** 同一种情况,需要找出最高分,最低分的学生的详细信息 求平均分 */ class StudentTest4 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[50]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } //找出三年级的最高分和最低分的学生,求平均分 //这是找对象,对于引用初始值可以赋null。 Student maxScoreOf3 = null; Student minScoreOf3 = null; double sumScore = 0; int count = 0; for(int i = 0; i < stuArr.length; i++) { if(stuArr[i].getGrade() == 3) { //获取最高分的学生 if(maxScoreOf3 == null) { maxScoreOf3 = stuArr[i]; } else if(stuArr[i].getScore() > maxScoreOf3.getScore()){ maxScoreOf3 = stuArr[i]; } //获取最低分的学生 if(minScoreOf3 == null) { minScoreOf3 = stuArr[i]; } else if(stuArr[i].getScore() < minScoreOf3.getScore()){ minScoreOf3 = stuArr[i]; } count++; sumScore += stuArr[i].getScore(); } } if(count == 0) { System.out.println("没有三年级的学生"); } else{ double avg = sumScore / count; System.out.println("三年级中的最高分是:" + maxScoreOf3.say()); System.out.println("三年级中的最低分是:" + minScoreOf3.say()); System.out.println("三年级中的平均分是:" + avg); } } } /** 将学生数组进行复制 */ class StudentTest5 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[20]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("----------------数组的复制------------------"); //数组的复制 /* 对于引用类型来说,只是将数组元素(引用类型)中保存的地址进行复制,对象还是那些 */ Student[] newArr = new Student[stuArr.length]; for(int i = 0; i < newArr.length; i++) { //每个数组元素保存的对象地址,copy一份写入新的数组元素中 newArr[i] = stuArr[i]; } //遍历 for (Student stu : newArr) { System.out.println(stu.say()); } } } /** 获取子数组(子数组的元素个数是原数组的一半) */ class StudentTest6 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[20]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------获取子数组(子数组的元素个数是原数组的一半)--------------------"); Student[] stuArrChild = new Student[stuArr.length / 2]; //遍历的时候以容量小的数组长度为准 for(int i = 0; i < stuArrChild.length; i++) { stuArrChild[i] = stuArr[i]; } //遍历 for (Student stu : stuArrChild) { System.out.println(stu.say()); } } } /** 给学生数组扩容 1.创建比原数组长度长的数组 2.复制原数组信息到新数组 3.修改指向,将原来数组名指向新的数组 */ class StudentTest7 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[20]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------给学生数组扩容--------------------"); Student[] stuArrBigger = new Student[stuArr.length * 2]; for(int i = 0; i < stuArr.length; i++) { stuArrBigger[i] = stuArr[i]; } //修改指向 stuArr = stuArrBigger; //遍历 for (Student stu : stuArr) { if(stu != null) { System.out.println(stu.say()); //扩容以后有null所以不能直接调用对象的方法 } else { System.out.println(stu); } } } } /* 子数组的练习 */ class StudentTest8 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[20]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------获取原来的1/3子数组--------------------"); Student[] stuArrChild = new Student[stuArr.length / 3]; for(int i = 0; i < stuArrChild.length; i++) { stuArrChild[i] = stuArr[i]; } //遍历 for (Student stu : stuArrChild) { System.out.println(stu.say()); } } } /* 数组扩容的练习 */ class StudentTest9 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[20]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------给学生数组扩容1.5倍--------------------"); Student[] stuArrBigger = new Student[(int)(stuArr.length * 1.5)]; for(int i = 0; i < stuArr.length; i++) { stuArrBigger[i] = stuArr[i]; } //修改指向 stuArr = stuArrBigger; //遍历 for (Student stu : stuArr) { if(stu != null) { System.out.println(stu.say()); //扩容以后有null所以不能直接调用对象的方法 } else { System.out.println(stu); } } } } /** 从数组中获取值的练习 把三年级的学生提取出来 1.先创建一个跟原数组一样大小的数组 2.将原数组中符合条件的复制到新数组中并计数(注意,在复制过程中原数组和新数组的索引不同) 3.将新数组利用计数的结果复制生成最终数组 */ class StudentTest10 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[50]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------把三年级的学生提取出来--------------------"); Student[] newStuArr = new Student[stuArr.length]; int count = 0; //先复制生成新数组 for(int i = 0; i < newStuArr.length; i++) { if(stuArr[i].getGrade() == 3) { newStuArr[count] = stuArr[i]; count++; } } //复制生成最终数组 Student[] stuArrFinal = new Student[count]; for(int i = 0; i < stuArrFinal.length; i++) { stuArrFinal[i] = newStuArr[i]; } //遍历 for (Student stu : stuArrFinal) { System.out.println(stu.say()); } } } /** 数组反转 */ class StudentTest11 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[10]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------数组反转--------------------"); for (int i = 0; i < (stuArr.length / 2); i++) { Student stu = stuArr[i]; stuArr[i] = stuArr[stuArr.length - 1 - i]; stuArr[stuArr.length - 1 - i] = stu; } //遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } } } /** 数组排序 */ class StudentTest12 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[10]; //利用for循环对数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------学生按照成绩从低到高排序--------------------"); //冒泡排序,外层控制趟数 length - 1 /* 若共有n个元素需要排序 趟数 需要比较的元素个数 1 n-1 2 n-2 3 n-3 n-1 n-(n-1)=1 所以共n-1趟 */ for (int i = 0; i < stuArr.length - 1; i++) { //每次比较的元素个数 for(int j = 0; j < stuArr.length - 1 - i; j++) { if(stuArr[j].getScore() > stuArr[j + 1].getScore()) { //交换 Student stu = stuArr[j]; stuArr[j] = stuArr[j + 1]; stuArr[j + 1] = stu; } } } //增强for循环,遍历数组, for (Student stu : stuArr) { System.out.println(stu.say()); } } } /* 学生按照成绩从高到低排序 */ class StudentTest13 { public static void main(String[] args) { //姓名库 String[] name1 = {"张", "李", "赵", "徐", "贾"}; String[] name2 = {"杰", "伟", "刚", "丽", "琳", "云"}; //创建数组 Student[] stuArr = new Student[10]; //for循环遍历给数组进行赋值 for (int i = 0; i < stuArr.length; i++) { int id = i + 1; int randIndex1 = (int)(Math.random() * name1.length); int randIndex2 = (int)(Math.random() * name2.length); String name = name1[randIndex1] + name2[randIndex2]; int grade = (int)(Math.random() * 6 + 1); double score = (int)(Math.random() * 101); stuArr[i] = new Student(id, name, grade, score); } // 遍历 for (Student stu : stuArr) { System.out.println(stu.say()); } System.out.println("--------------学生按照成绩从高到低排序--------------------"); //外层循环控制趟数 for (int i = 0; i < stuArr.length - 1; i++) { //内层循环控制比较的元素个数 for(int j = 0; j < stuArr.length - 1 - i; j++) { //交换 if(stuArr[j].getScore() < stuArr[j + 1].getScore()) { Student stu = stuArr[j]; stuArr[j] = stuArr[j + 1]; stuArr[j + 1] = stu; } } } //增强for循环,遍历数组, for (Student stu : stuArr) { System.out.println(stu.say()); } } }
简单粗暴,直接贴一个大型代码块,满足所有需求。
详情参考数组玩法(一)
留言一起学习,一起进步。
相关文章推荐
- 数组玩法(1):下标移位
- 数组玩法(1):下标移位
- 枪打露头鸟---数组的玩法
- 数组玩法(一)
- C#中 combobox 如何绑定数组
- 【转】 C 语言中数组和指针的关系
- 百度改错题:统计数组中a-z的个数
- poj 2195 Going Home--最小费用最大流--spfa--动态数组--或者用 最小权匹配
- Json数组存入数据库与前台JS解析
- 第十五周 项目一:建立专门的数组类处理有关数组的操作
- c语言字符数组大小写转换
- c 语言的例题 数组的 经典题
- 数组-01. 字符转换(15)
- 约瑟夫环的数组
- 数组排序之对一组学生按照成绩, 学号,年龄, 姓名排序~
- C语言数组使用
- poj 3666 数组整单调 dp
- jquery $.each遍历json数组方法
- js数组操作
- js 数组操作