Java object array example: define student objects and sort students

Java study notes (source from Shang Silicon Valley)

Introduction

The main purpose of this article is to write a StudenTest object to achieve the following functions:
create 20 student objects with student IDs from 1 to 20, and grades and grades are determined by random numbers.
One: Print out the student information of Grade 3 (state value is 3).
Two: Use bubble sorting to sort by student performance and traverse all student information

New object

package com.atguigu.cntact;


public class StudentTest {
    
    
	public static void main(String[] args) {
    
    

		//声明Student类型的数组,声明对象类型的数组时,后面要加上[],这儿是动态赋值方式
		Student[] stus = new Student[20];  //String[] arr = new String[10];
		
		for(int i = 0;i < stus.length;i++){
    
    
			//给数组元素赋值
			stus[i] = new Student();
			//给Student对象的属性赋值
			stus[i].number = (i + 1);
			//年级:[1,6]
			stus[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);    //因为这儿有截断,因此把六点几转换成六,要多加个1
			//成绩:[0,100]
			stus[i].score = (int)(Math.random() * (100 - 0 + 1));
		}
		
		//遍历学生数组
		for(int i = 0;i <stus.length;i++){
    
    
//			System.out.println(stus[i].number + "," + stus[i].state 
//					+ "," + stus[i].score);
			
			System.out.println(stus[i].info());      用Alt+
		}
		
		System.out.println("********************");
		for(int i = 0;i <stus.length;i++){
    
    
			if(stus[i].state == 3){
    
    
				System.out.println(stus[i].info());  
//Eclipse环境中,输入syso按Alt+/,能够有提示,/必须是shift旁的那个,不能是小键盘那个,否则不起作用,注释按键ctrl+/也必须是该斜线
			}
		}
		
		System.out.println("********************");
	}
}

class Student{
    
    
	int number;//学号
	int state;//年级
	int score;//成绩
	
	//显示学生信息的方法
	public String info(){
    
    
		return "学号:" + number + ",年级:" + state + ",成绩:" + score;
	}
	
}

Code explanation

The result of running the code is

Student ID: 1, Grade: 3 Scores: 70
Student ID: 2, Grade: 2, results: 30
Student ID: 3, Grade: 6, results: 83
Science NO: 4, Grade: 6, results: 100
Science No. : 5, grade: 6, grade: 56
student number: 6, grade: 4, grade: 32
student number: 7, grade: 3, grade: 30
student number: 8, grade: 6, grade: 2
student number: 9 , Grade: 5, Grade: 67
Student Number: 10, Grade: 4, Grade: 96
Student Number: 11, Grade: 6, Grade: 27
Student Number: 12, Grade: 2, Grade: 93
Student Number: 13, Grade : 4, grade: 65,
student number: 14, grade: 4, grade: 0,
student number: 15, grade: 4, grade: 98,
student number: 16, grade: 2, grade: 76,
student number: 17, grade: 2 , Grade: 82,
student number: 18, grade: 3, grade: 45,
student number: 19, grade: 6, grade: 24,
student number: 20, grade: 5, grade: 25


Student ID: 1, Grade: 3, Grade: 70,
Student ID: 7, Grade: 3, Grade: 30,
Student ID: 18, Grade: 3, Grade: 45


On the left is the stack, on the right is the heap
The left is the stack, the right is the heap, stus is in the stack, and the heap has a new array, the length is 20, the value of each position is null , and then the value of each position is assigned.
First create a student object and create them separately Int's student number, age and grade, after the new object, the system assigns 0 to the attribute of the object by default.
Then create a method in the object to facilitate the direct call of the student object method in the main function.
ps: After the assignment of the array in the heap, the address of the object is stored, pointing to the properties and methods of the object in the heap space.

Math.random()
Math.random() by default generates random double random numbers greater than or equal to 0.0 and less than 1.0,
that is: 0.0<=Math.random()<1.0
but any random number can be generated with a little processing
link

stus[i] = new Student();
			//给Student对象的属性赋值
			stus[i].number = (i + 1);

The student[] array itself is a reference data type, and the elements of the array are a class, that is, a custom reference data type. Assigning stus[i] must be an object of the stus class. Since it is an object, it must be a new one.

public class MathRandom {
    
    
 
    public static int getRandomInt(int num1,int num2)
    {
    
    
        int n=num1+(int)(Math.random()*(num2-num1));
        return n;
    }
 
    public static char getRandomChar(char ch1,char ch2)
    {
    
    
        char ch=(char) (ch1+ Math.random()*(ch2-ch1+1));
        return ch;
    }
 
    public static void main(String args[])
    {
    
    
        System.out.println(MathRandom.getRandomInt(1,7)); //产生的随机数包括1,不包括7
        System.out.println(MathRandom.getRandomChar('a','b'));//a和b都包括
    }
}

Adding an (int) is a type conversion, truncation. Therefore, the grade state needs to add an extra 1 to make the six-point age stage into the sixth grade.

Bubble Sort

code show as below

//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
		for(int i = 0;i < stus.length - 1;i++){
    
    
			for(int j = 0;j < stus.length - 1 - i;j++){
    
    
				if(stus[j].score > stus[j + 1].score){
    
    
					//如果需要换序,交换的是数组的元素:Student对象!!!
					Student temp = stus[j];
					stus[j] = stus[j + 1];
					stus[j + 1] = temp;
				}
			}
		}

The outer for loop refers to the number of bubble sorting, n data will be compared n-1 times, the first time to find the maximum value, the second time to find the second largest value; the inner for loop refers to The number of comparisons for each sorting, each time the outer layer finds a maximum value, the inner layer will compare it once less.

Guess you like

Origin blog.csdn.net/Meloneating/article/details/112954048