java基础:面向对象编程12-对象数组

这篇博客主要介绍了如何在Java中定义一个Student类,包含学号、年级和成绩属性。通过创建20个随机生成的学生对象,分别解决两个问题:筛选出3年级学生并打印信息,以及使用冒泡排序按成绩对所有学生进行排序并显示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
			
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值