(爪哇)当对象位于 ArrayList 中时,如何打印对象的信息?



我在main中有一个ArrayList,我有一个类,里面有一个构造函数和一个打印数据的方法。当被调用时,我会添加一个带有新信息的新对象,并将其添加到ArrayList中以将其保存在一个位置。我遇到困难的是打印信息的语法。我用常规数组尝试过,但我需要使用ArrayList。我需要能够获得特定对象的索引,并打印该对象的信息。例如,最后几行下面的代码:

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?

}
}

如有任何帮助或澄清,我们将不胜感激。

您需要将listOfObj的定义从:更改为

ArrayList listOfObj = new ArrayList();

至:

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

第一个将创建Object类对象的ArrayList

第二个将创建student类对象的ArrayList

你的代码中还有几个问题:

  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块正确处理异常
  • 您可以更改ArrayList的定义并在学生中添加到String((类
  • 打印所有的学生对象而不是用于循环使用一个sop

EX:-

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]]

最新更新