1.数组
一维数组
数组是一个数据容器,可以存储一批同类型数据
数组类型[ ] 数组名={元素1,元素2,....}
数组类型[ ] 数组名=new 数据类型[ ]{元素1,元素2,....}
也可写成 数组类型 数组名[ ]
数据的访问
索引从0开始
数组名[索引]
获得数组长度(元素个数)
数组名.length
//开发一个随机点名的方法,假设有15个学生
public static void randomName(){
//1.创建数组
String[] names = {"张三","李四","王五","赵六","田七","宋八","周九","吴十","郑十一","王十二","王十三","王十四","王十五","王十六","王十七"};
//2.随机数 Math.random() ---->[0,1)
int index = (int)(Math.random()*names.length);
//3.输出
System.out.println(names[index]);
}
案例:计算班级学生Java成绩信息,要求输出平均分,最高分,最低分
假设班级有8名学生,请帮忙开发程序录入8名学生的Java成绩,成绩类型是小数,并输出平均分,最高分,最低分
public static void getScore(int n){
double[] scores = new double[n];
double sum = 0;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.println("请输入第"+(i+1)+"个学生的成绩");
scores[i] = sc.nextDouble();
sum += scores[i];
}
//平均分
System.out.println("平均分:"+sum/scores.length);
//排序
double max = scores[0];
double min = scores[0];
for (int i = 1; i < scores.length; i++) {
if(scores[i]>max){
max = scores[i];
}
if(scores[i]<min){
min = scores[i];
}
}
//最高分
System.out.println("最高分:"+max);
//最低分
System.out.println("最低分:"+min);
}
//案例 斗地主游戏 //开发一个简易版斗地主游戏,要求只完成做牌(存储54张牌),洗牌
public static void playDoudizhu(){
//1.创建数组,存储54张牌
String[] Jokers ={"🃏","🤡"};
String[] colors = {"♥","♠","♣","♦"};
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
String[] cards = new String[54];
//2.遍历数组,将牌存储到cards数组中
int index = 0;
for (String color : colors) {
for (String number : numbers) {
cards[index] = color+number;
index++;
}
}
for (String joker : Jokers) {
cards[index] = joker;
index++;
}
//遍历牌
for (String card : cards) {
System.out.print(card+" ");
}
System.out.println();
//洗牌
for (int i = 0; i < cards.length; i++) {
int index1 = (int)(Math.random()*cards.length);
int index2 = (int)(Math.random()*cards.length);
String temp = cards[index1];
cards[index1] = cards[index2];
cards[index2] = temp;
}
//洗牌后,遍历
for (String card : cards) {
System.out.print(card+" ");
}
}
二维数组(五子棋,坦克大战游戏)
静态:
数组类型[ ][ ]数组名=new 数组类型[ ][ ]{元素1,元素2,....}
动态:
数组类型[ ][ ]数组名=new 数组类型[行][列]
行列矩阵
需求:开发一个程序可以记录班级学生的座位信息
存储班级座位上的学生名称,并要求输出学生名称时,能直观看到学生所在的位置
String[][] positions = {
{"张无忌", "赵敏", "周芷若"},
{"张三丰", "孙悟空", "唐僧"},
{"吴彦祖", "猪八戒", "沙和尚"},
{"杨晓", "菩提老祖"}
};
for (int i = 0; i < positions.length; i++) {
for (int j = 0; j < positions[i].length; j++) {
System.out.print(positions[i][j] + "\t");
}
System.out.println();
}
System.out.println(positions[1][1]);//孙悟空
System.out.println(positions.length);//4
System.out.println(positions[3].length);//2
案例:石头迷阵游戏
//初始化数据
//打乱数据顺序
//恢复
//创建二维数组
public static void games(int n) {
int[][] arr = new int[n][n];
int num=1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = num;
num++;
}
}
print(arr);
System.out.println("====================");
shuffle(arr);
print(arr);
}
//遍历
public static void print(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
//打乱顺序
public static void shuffle(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
int i1 = (int) (Math.random() * arr.length);
int j1 = (int) (Math.random() * arr[i].length);
int temp = arr[i][j];
arr[i][j] = arr[i1][j1];
arr[i1][j1] = temp;
}
}
}
2.面向对象编程---封装,继承,多态
存储数据:变量 数组 对象
对象是一种特殊的数据结构(可以理解为一张表),用来记住一个事务的数据,从而代表该事物
public class Student {
String name;
double chinese;
double math;
//求总和
public double getSum(){
return chinese+math;
}
//求平均分
public double getAvg(){
return getSum()/2;
}
}
Student s1=new Student();
s1.name="波妞";
s1.chinese=100;
s1.math=100;
Student s2=new Student();
s2.name="波仔";
s2.chinese=59;
s2.math=100;
System.out.println(s1.getSum());
System.out.println(s2.getSum());
System.out.println(s1.getAvg());
System.out.println(s2.getAvg());
方法区放类文件class
对象放在堆内存中
类的基本语法
构造器---对象初始化
默认带一个无参构造器
如果定义了一个有参构造器,默认的无参构造器就没有了,要重新创建一个无参构造器
修饰符 类名(){
...
}
//无参构造器
public Student(){}
//有参构造器
public Student(String name,double chinese,double math){
this.name=name;
this.chinese=chinese;
this.math=math;
}
Student s1=new Student();
s1.name="波妞";
s1.chinese=100;
s1.math=100;
Student s2=new Student("波仔",59,100);
this关键字---解决成员变量与局部变量名称冲突的问题
this就是一个变量,可以方法中,当成当前对象
哪个对象调用这个方法,this就是哪个对象
可以访问对象数据
封装
合理隐藏private,合理暴露public
成员变量私有
public class Student {
private String name;
private double chinese;
private double math;
//构造器
//无参构造器
public Student(){
System.