java学习day19---(数组)
项目:相当于一个程序
模块:程序里的各种功能,如:支付模块、显示模块、存储模块
包:模块里创建包,每个包里面归纳不同类型的类,如:实例类包、抽象类包、工具类包等
类:具体的代码
规范创建项目到类:
1创建一个空的项目
2.创建一个模块
3.创建具体的包等等
jvm内存大概:
二、数组
对批量数据进行表示和计算的时候,需要一个一个创建变量,很不方便,因此需要创建数组
数组是一种数据结构
数据结构是存储数据的各种方式
数组概念
数组是一块连续的存储空间,用来存放相同类型的数据
语法:类型[] 变量名=字面值; 或者 类型 变量名[]=字面值;
元素:每个存储在数组独立的数据 索引:对每个数据起一个标号,从0到数组长度-1
数组的本质就是一块连续的存储空间
数组可以存储基本数据类型、引用类型,类型必须一致(包括自动转换),如:double数组可以存储int类型,因为int类型能够自动转换成double类型
优缺点,都是源于地址连续,查询快,但是效率低(增删慢)
动态初始化,开辟空间但不赋值,有默认值 :类型[] 变量名=new 类型[长度];
静态创建:类型[] 变量名={1,2,3,4};
数组遍历
length 获得数组长度 数组名.length =数组长度
使用for循环遍历数组:
for(int i=0;i<数组名.length;i++){}
三、一些编程的思想
1.考虑健壮性 健壮性:在考虑问题的解决方案时,尽可能的考虑到各种情况并给出对应的处理解决方案 考虑返回结果为数组就是健壮性
2.将功能需求抽取为方法编写,不要所有的代码堆在一个方法中,或者main方法中
好处:1.明显感觉封装的好处,结构非常清晰 2.可以做到复用 3.错误很好排查 4.培养分析问题的参数和返回值的能力
3.将静态初始化和动态初始化的场景理清
4.链式编程:直接把方法的返回值结果作为参数,或者直接赋值 如: int number=fun();
5.以结果为导向分析,从结果需要那些数据,分析这些数据是否能够拿到,如果拿不到再来分析如果得到这些数据
6.直接操作原生数组数据结构其实比较繁琐的 (1)数组创建需要必须制定长度,先计算需要的长度,才能创建数组 (2)自己扩容也很不方便 (3)数组每做一个功能离不开遍历一次
public class Student { private String name; private double score; public Student(String name, int score) { this.name = name; this.score = score; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return score == student.score && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, score); } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", score=" + score + '}'; } public static void through(Student[] arr){ //遍历Student类型的数组 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
测试类:
public class Test { public static void main(String[] args) { Student stu1=new Student("欧阳修",78); Student stu2=new Student("王安石",89); Student stu3=new Student("韩愈",69); Student stu4=new Student("苏轼",100); Student stu5=new Student("苏洵",85); Student stu6=new Student("苏辙",94); Student stu7=new Student("柳宗元",91); Student stu8=new Student("曾巩",88); Student[] arrStudent={stu1,stu2,stu3,stu4,stu5,stu6,stu7,stu8}; Student[] people=averageNumber(arrStudent); Student.through(people); //遍历Student类型的数组 } public static Student[] averageNumber(Student[] arr){ double average=stuAverage(arr); //得到平均分 int count=stuExceedAverage(arr,average); //得到人数 Student[] averageStudent=new Student[count]; //创建新的数组 stuPerson(arr,averageStudent,average); //把大于的对象放入新创建的数组 return averageStudent; } public static double stuAverage(Student[] arr){ //求平均分 double average=0; for (int i = 0; i < arr.length; i++) { average+=arr[i].getScore(); } average/=arr.length; System.out.println("平均分为:"+average); return average; } public static int stuExceedAverage(Student[] arr,double average){ //求大于平均分的对象 int x=0; for (int i = 0; i < arr.length; i++) { if(arr[i].getScore()>=average){ x++; } } return x; } public static void stuPerson(Student[] arr,Student[] averageStudent,double average){ //把对象放入一个新的数组 int x=0; for (int i = 0; i < arr.length; i++) { if(arr[i].getScore()>=average){ averageStudent[x]=arr[i]; x++; } } } }

浙公网安备 33010602011771号