The difference between using ArrayList and not using ArrayList in Java-storing student objects and traversing

The difference between using ArrayList and not using ArrayList in Java-storing student objects and traversing

basic introduction

  1. What is a collection?
    Provide a storage model with variable storage space, and the stored data capacity can be changed
  2. The characteristics of the ArrayList collection The
    bottom layer is realized by an array, the length can be changed
  3. The use of generics is used
    to constrain the data type of the elements stored in the collection

Subject requirements

Create a collection to store student objects, store 3 student objects, and use the program to traverse the collection on the console. The
student’s name and age come from keyboard entry

Research code

Student.java

The first is to write a Student class, and later use

package study01;

public class Student {
    
    
    private String name;
    private String age;

    public Student() {
    
    
    }

    public Student(String name, String age) {
    
    
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public String getName() {
    
    
        return name;
    }

    public void setAge(String age) {
    
    
        this.age = age;
    }

    public String getAge() {
    
    
        return age;
    }
}

MyArrayList.java

The following three different methods are used for comparison, which is easy for us to understand, and interested students can take a look:

package study01;

import java.util.ArrayList;
import java.util.Scanner;

public class MyArrayList {
    
    
    public static void main(String[] args) {
    
    
        
        //1.普通方法
        
        Student s1 = new Student();
        Student s2 = new Student();
        Student s3 = new Student();
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("请输入学生姓名:");
        s1.setName(sc.nextLine());
        System.out.println("请输入学生年龄:");
        s1.setAge(sc.nextLine());
        
        System.out.println("请输入学生姓名:");
        s2.setName(sc.nextLine());
        System.out.println("请输入学生年龄:");
        s2.setAge(sc.nextLine());
        
        System.out.println("请输入学生姓名:");
        s3.setName(sc.nextLine());
        System.out.println("请输入学生年龄:");
        s3.setAge(sc.nextLine());
        
        System.out.println(s1.getName() + " " + s1.getAge());
        System.out.println(s2.getName() + " " + s2.getAge());
        System.out.println(s3.getName() + " " + s3.getAge());
        
        
        //2.运用ArrayList
        
        ArrayList<Student> array = new ArrayList<Student>();
//        Student s1 = new Student();
//        Student s2 = new Student();
//        Student s3 = new Student();
//        Scanner sc = new Scanner(System.in);
        
        System.out.println("请输入学生姓名:");
        s1.setName(sc.nextLine());
        System.out.println("请输入学生年龄:");
        s1.setAge(sc.nextLine());
        
        System.out.println("请输入学生姓名:");
        s2.setName(sc.nextLine());
        System.out.println("请输入学生年龄:");
        s2.setAge(sc.nextLine());
        
        System.out.println("请输入学生姓名:");
        s3.setName(sc.nextLine());
        System.out.println("请输入学生年龄:");
        s3.setAge(sc.nextLine());
        
        array.add(s1);
        array.add(s2);
        array.add(s3);
        
        for (int i = 0; i < array.size(); i++) {
    
    
            Student s = array.get(i);
            System.out.println(s.getName() + "," + s.getAge());
        }
        
        
        //3.高效的ArrayList方法
        
        ArrayList<Student> array1 = new ArrayList<Student>();
        for (int i = 0; i < 3; i++) {
    
    
            addStudent(array1);
        }
        for (int i = 0; i < array1.size(); i++) {
    
    
            Student st2 = array1.get(i);
            System.out.println(st2.getName() + " " + st2.getAge());
            
        }
    }

    //这个方法是定义在(3.高效的ArrayList方法)后面的
    public static void addStudent(ArrayList<Student> array1) {
    
    
        Scanner sc1 = new Scanner(System.in);

        System.out.println("请输入学生姓名:");
        String name = sc1.nextLine();

        System.out.println("请输入学生年龄:");
        String age = sc1.nextLine();

        Student st1 = new Student();
        st1.setName(name);
        st1.setAge(age);
        array1.add(st1);
    }
}

The results of the operation are given below:

Insert picture description here
Insert picture description here
If it's helpful to you, you might as well give a like and support it!

Guess you like

Origin blog.csdn.net/Freedom_cao/article/details/108565564