如何从 Java 中的数组列表返回特定对象(索引)? [英] How can I return a specific object (index) from an arraylist in Java?
问题描述
我创建了一个名为 Student 的类并为其分配了 3 个值(StudentNumber、Course、Mark);然后我将一个创建的学生分配给一个名为 studentCourses 的数组列表.
I created a class called Student and assigned it 3 values (StudentNumber, Course, Mark); I then assigned a created student (s) to an arrayList called studentsCourses.
这样每个学生就占据了数组中的一个索引.但是我如何让学生们从 arrayList 中退出……如果这有意义的话?我已经存储了它们,但我不知道如何回忆这些信息以便我可以看到它.
This way each student takes up an index in the array. But how do I get the students back OUT of the arrayList... if that makes sense? I have them stored, but I don't know how to recall the information so that I can see it.
public static void main (String[]args){
ArrayList<Student> studentsCourses= new ArrayList<Student>();
String studentNumber="~";
String course="~";
int mark=0;
Student pat;
Student s=null;
do{
s=new Student();
System.out.println("Please Enter Your Student ID (type quit to Quit):\n");
studentNumber=keybrd.next();
s.setStudentNumber(studentNumber);
System.out.println("Please Enter Your Course ID:\n");
course=keybrd.next();
s.setCourse(course);
System.out.println("Please Enter Your Mark:\n");
mark=keybrd.nextInt();
s.setMark(mark);
s.printStates();
studentsCourses.add(s);
}while(!studentNumber.equals("quit"));
System.out.println(studentsCourses.size());
System.out.println(studentsCourses);
所以如果我想为创建的学生 (pat) 分配索引 1 的状态,我该怎么做?
so if I wanted to assign the created student (pat) the state of index 1 how would I do that?
我的另一个问题(与这段代码一起)是我的哨兵.当我输入quit时它确实退出了.但它首先完成了do"的运行.这意味着我实际上最终得到了一个学生,他的编号是退出"课程是什么,而分数是什么.
My one other issue (going along with this piece of code) is my sentinel. it does indeed quit when I type quit. but it finishes running through the "do" first. meaning I actually end up with a student who's number is "quit" course is whatever and mark is whatever.
如何在输入quit"后立即强制它中断 do?
How do I force it to break the do as soon as "quit" is typed?
推荐答案
"但是我如何让学生从 arrayList 中退出...回忆起信息以便我可以看到它."
"But how do I get the students back OUT of the arrayList... if that makes sense? I have them stored, but I don't know how to recall the information so that I can see it."
从数组列表中获取数据:
To get the data from arraylist:
for (Student student : studentsCourses) {
System.out.println(student.getStudentNumber());
System.out.println(student.getCourse());
System.out.println(student.getMarkr());
}
或
for (int i = 0; i < studentsCourses.size(); i++) {
Student student = studentsCourses.get(i);
System.out.println(student.getStudentNumber());
System.out.println(student.getCourse());
System.out.println(student.getMarkr());
}
要解决其他问题,您可以尝试:
to solve other issue, you can try:
do{
s=new Student();
System.out.println("Please Enter Your Student ID (type quit to Quit):\n");
studentNumber=keybrd.next();
if(studentNumber.equals("quit"))
break;
s.setStudentNumber(studentNumber);
System.out.println("Please Enter Your Course ID:\n");
course=keybrd.next();
s.setCourse(course);
System.out.println("Please Enter Your Mark:\n");
mark=keybrd.nextInt();
s.setMark(mark);
s.printStates();
studentsCourses.add(s);
}while(true);
这篇关于如何从 Java 中的数组列表返回特定对象(索引)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!