(Java)如何我可以打印的对象的信息时,对象是在对列表? [重复]

0

的问题

我有一个。在主要和我有一类与内部构造它的方法和打印数据。 我添加一个新的物体与新的信息,在调,并将它添加到数组列表,以保持它在一个地方。 什么我有一个很难的语法,以打印的信息。 我试着与一个经常阵列,但我需要使用。. 我需要能够得到的指标的特定对象和打印该对象的信息。 例如,代码下面的最后两行:

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

public class student{

    String name;
    int age;
    int birthYear;

    public student(String name, int age, int birthYear){
        this.name = name;
        this.age = age;
        this.birthYear = birthYear;
    }
    
    public void printStudentInformation(){
        System.out.println(name);
        System.out.println(age);
        System.out.println(birthYear);
    }
}

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

        ArrayList listOfObj = new ArrayList();
        ArrayList names = new ArrayList();
        Scanner sc = new Scanner(System.in);

        for(int i = 0; i < 3; i++){

            System.out.println("New Student Information:"); // Three student's information will be saved
            String name = sc.nextLine();
            int age = sc.nextInt();
            int birthYear = sc.nextInt();

            student someStudent = new student(name, age, birthYear);
            listOfObj.add(someStudent);
            names.add(name);
        }

        System.out.println("What student's information do you wish to view?");
        for(int i = 0; i < names.size(); i++){
            System.out.println((i + 1) + ") " + names.get(i)); // Prints all students starting from 1
        }
        int chosenStudent = sc.nextInt(); // Choose a number that correlates to a student
        
        // Should print out chosen student's object information
        listOfObj.get(chosenStudent).printStudentInformation(); // This is incorrect, but I would think the syntax would be similar?
        
    }
}

任何帮助或澄清是极大的赞赏。

arraylist java printing
2021-11-24 04:07:52
3
1

你需要改变您的定义 listOfObj 自:

ArrayList listOfObj = new ArrayList();

为:

ArrayList<student> listOfObj = new ArrayList<>();

第一会将创建一个 ArrayListObject 类对象。

第二,将创建一个 ArrayListstudent 类对象。

几个问题,在你的代码:

  1. 因为你正在读的名字使用 nextLine你可能需要跳过一个新的线阅读的出生年份,如:
...
int birthYear = sc.nextInt();
sc.nextLine();  // Otherwise in the next loop iteration, it will skip reading input and throw some exception
...
  1. 你选择一个选项对于学生来显示,但该选项1和索引 ArrayList 商店0编制索引,所以你应该改变的行为 sc.nextInt() - 1:
int chosenStudent = sc.nextInt() - 1; // Choose a number that correlates to a student
  1. Scanner 可能扔在例外情况,例如,串而不是一个int. 所以确保你处理异常正确使用 try-catch 块。
2021-11-24 04:26:42
1
  • 你改变。定义和加toString()在学员 类。
  • 和打印所有学生的对象而不是使用对环使用的只是 一个sop。

例如:-

import java.util.*;

class Student {
    private String name;
    private int age;
    private int birthYear;

    public Student() {
        super();
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "Student [age=" + age + ", birthYear=" + birthYear + ", name=" + name + "]";
    }

}

public class DemoArrayList {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();

        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();

        for (int i = 0; i < n; i++) {
            scan.nextLine();
            String name = scan.nextLine();
            int age = scan.nextInt();
            int birthYear = scan.nextInt();
            list.add(new Student(name, age, birthYear));
        }

        System.out.println(list);
    }
}

O/P:-

2
joy 
10
2003
jay
20
2005
[Student [age=10, birthYear=2003, name=joy], Student [age=20, birthYear=2005, name=jay]]
2021-11-24 04:14:02

嗨,你可以编辑的答案,并添加一些代码,以便作可以更好地理解它?
kiner_shah

好只要等待一些时间
Batek'S

现在看看这个代码。
Batek'S
0

你不能宣布类型的 ArrayList 虽然初始化 listOfObjnamesArrayList.

这里是正确的方式宣告对列表:

ArrayList<type> list = new ArrayList();

和其他的问题是虽然你获取的数据对列表. 变量 chosenStudent 采取的位置,从用户和从中获取数据 listOfObj 但是开始从指数 01.

例如你的输入是 2 然后,对列表获得的数据从第二的位置,但你指的是开始从 1 同时打印数据。 你必须把 chosenStudent - 1 为获得正确数据。

listOfObj.get(chosenStudent - 1).printStudentInformation() // chosenStudent = 2 - 1 = 1 

这里是我的代码:

ArrayList<student> listOfObj = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
Scanner sc = new Scanner(System.in);
    for(int i = 0; i < 3; i++){
        System.out.println("New Student Information:");
        String name = sc.next();
        int age = sc.nextInt();
        int birthYear = sc.nextInt();

        student someStudent = new student(name, age, birthYear);
        listOfObj.add(someStudent);
        names.add(name);
     }

     System.out.println("What student's information do you wish to view?");
     for(int i = 0; i < names.size(); i++){
        System.out.println((i + 1) + ") " + names.get(i));
     }
     int chosenStudent = sc.nextInt();
        
     listOfObj.get(chosenStudent - 1).printStudentInformation();
2021-11-24 04:41:01

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................