package com.atguigu.java3;
import org.omg.CORBA.COMM_FAILURE;
/*
* 对象数组:数组中存储的是对象
*/
class Computer {
String type;
}
public class ObjectArrayTest {
public static void main(String[] args) {
ObjectArrayTest ot = new ObjectArrayTest();
// ot.demo1();
// ot.demo2();
ot.demo3();
}
public void demo3(){
//创建数组
Computer[] cs = new Computer[3];
//遍历数组给数组赋值
for (int i = 0; i < cs.length; i++) {
Computer c = new Computer(); //沒循環一次都創建一個不同的對象
c.type="小龙哥";
cs[i]= c;
}
cs[0].type="清华同方";
for (int i = 0; i < cs.length; i++) {
System.out.println(cs[i].type);
}
}
/*
* 数组中所有元素存储同一个对象
*/
public void demo2(){
//创建数组
Computer[] cs = new Computer[3];
//创建对像
Computer computer = new Computer();
computer.type = "拯救者";
//遍历数组给数组赋值
for (int i = 0; i < cs.length; i++) {
cs[i]= computer;
}
for (int i = 0; i < cs.length; i++) {
System.out.println(cs[i].type);
}
//修改索引值为0的对象的属性
cs[0].type= "雷神";
for (int i = 0; i < cs.length; i++) {
System.out.println(cs[i].type);
} //发现所有的数组元素的属性都被修改了。因为他们共享同一个对象的地址值。
}
/*
* 给数组中赋值对象,获取数组中的对象,修改数组中对象的属性值
*/
/* public void demo1(){
String[] ps =new String[3];
ps[0] = "ccc"; //在java中把所有双引号起来的都看成是string的对象(实例)
ps[1] = new String("aaa");
System.out.println("-----------------------------------");
Computer[] cs = new Computer[3];
System.out.println(cs[0]);//引用数据类型默认值是null
//向数组中存放对象
//1、创建对像
Computer c = new Computer();
c.type = "联想Y470";
//2、将对象放入数组中
cs[0] = c;
Computer c2 = new Computer();
cs[1] = c2;
//通过获取数组中的元素诶属性赋值
cs[1].type= "外星人";
}
*/
}
练习题一:
*定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1) 生成随机数:Math.random(),返回值类型double;
2) 四舍五入取整:Math.round(double d),返回值类型long
package com.atguigu.exer;
class Student{
int number;
int state;
int score;
}
public class ObjectArrayTest {
public static void main(String[] args) {
//创建数组
Student[] students =new Student[20];
for (int i = 0; i <students.length; i++) {
// 创建对象
Student s = new Student();
//给对象属性赋值
s.state = (int)(Math.random()*6+1); //(int)对后边括号里边的结果进行强制类型转换
s.number = i+1;
s.score = (int)(Math.round(Math.random() * 100)); //因为score 的类型是int,Math.round()返回的是long
//将对象赋值到数组
students[i] = s;
}
//遍历数组中的元素
for (int i = 0; i < students.length; i++) {
Student s = students[i];
if(s.state==3){
System.out.println(s.number+" "+s.state+" "+s.score);
}
}
}
}
输出结果:
练习题二:
使用冒泡排序按学生成绩排序,并遍历所有学生信息
package com.atguigu.exer;
class Student{
int number;
int state;
int score;
}
public class ObjectArrayTest {
public static void main(String[] args) {
//创建数组
Student[] students =new Student[20];
for (int i = 0; i <students.length; i++) {
// 创建对象
Student s = new Student();
//给对象属性赋值
s.state = (int)(Math.random()*6+1); //(int)对后边括号里边的结果进行强制类型转换
s.number = i+1;
s.score = (int)(Math.round(Math.random() * 100)); //因为score 的类型是int,Math.round()返回的是long
//将对象赋值到数组
students[i] = s;
}
for (int i = 0; i < students.length-1; i++) {
for (int j = 0; j < students.length-1-i; j++) {
if(students[j].score > students[j+1].score){
//交换- 这里交换的是对象不是属性
Student student = students[j];
students[j] = students[j+1];
students[j+1]=student;
}
}
}
//遍历数组中的元素
for (int i = 0; i < students.length; i++) {
Student s = students[i];
System.out.println(s.number+" "+s.state+" "+s.score);
}
}
}