如何管理/列出我的对象?

时间:2016-06-27 20:46:51

标签: java arraylist

在我的java代码中,我构建了一些学生类型的对象。 ---对象Student包含一个String名称和一个int等级--- 是否可以使用方法列出我创建的所有对象?

或者更确切地说是列出我的对象的所有名称而不是逐个添加它们?

4 个答案:

答案 0 :(得分:1)

当然,将它们全部放在某个集合中,如数组或列表,并使用for each迭代它,在一个返回循环中学生名称的方法上调用print。

代码示例:

List<Student> students = new ArrayList<>();
students.add(student1);
...
for (Student s : students) {
     System.out.println(s.getName());
}

答案 1 :(得分:1)

您可以将学生对象添加到集合/数组中然后遍历集合/数组并打印您喜欢的任何内容。

这是一个例子:

public class Student {
    private String name;
    private int grade; 

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

    public int getGrade() {
        return grade;
    }

    public String getName() {
        return name;
    } 
}

主类示例:

import java.util.ArrayList;

public class Main
{
    public static void main(String[] args) {
        //Creating a list of Student Objects
        ArrayList<Student> students = new ArrayList<>();    
        students.add(new Student("John",10));
        students.add(new Student("Ahmed",100));
        students.add(new Student("Jonny",10));

        //Printing the name of each student
        for(Student student:students){
            System.out.println(student.getName());
        }
        //Printing the grade of each student
        for(Student student:students){
            System.out.println(student.getGrade());
        }
    }
}

答案 2 :(得分:0)

  

是否可以使用方法列出我创建的所有对象?

是的,覆盖班级中的toString方法,然后创建学生列表

List<Student> myList = new ArrayList<Student>();

然后打印他们,调用System.out.println(myList );

答案 3 :(得分:0)

是的,你可以这样做。这应该是一个完整的例子。

import java.util.Scanner;

public class StudentDemonstration {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("How many student records to create : ");
        int numberStudents = in.nextInt();
        in.nextLine();
        Student students[] = new Student[numberStudents];
        for (int i=0; i<students.length; i++){
            students[i] = new Student();
            System.out.println("Enter name for student " + i + " : " );
            String name = in.nextLine();
            students[i].setName(name);
            System.out.println("Enter grade for student " + i + " : ");
            int grade = in.nextInt();
            in.nextLine();
            students[i].setGrade(grade);
        }
        System.out.println("Printing student details : " );
        for (int i=0; i<students.length; i++){
            System.out.println("Student details " + i);
            System.out.println("Name : " + students[i].name);
            System.out.println("Grade : " + students[i].grade);
        }
        in.close();
    }
}

class Student{
    String name;
    int grade;

    public Student(){
        this.name = null;
        this.grade = 0;
    }

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

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

    public void setGrade(int grade){
        this.grade = grade;
    }
}