如何从Java中的arraylist返回特定的对象(索引)? [英] How can I return a specific object (index) from an arraylist in Java?
问题描述
我创建了一个名为Student的类,并为其分配了3个值(StudentNumber,Course,Mark);然后,我将一个已创建的学生分配给一个名为"studentsCourses"的arrayList.
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);
所以如果我想为创建的学生(拍拍)分配索引1的状态,我该怎么做?
so if I wanted to assign the created student (pat) the state of index 1 how would I do that?
我的另一个问题(以及这段代码)是我的哨兵.当我键入退出时,它确实确实退出了.但先完成"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"后,如何强制其中断操作?
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."
要从arraylist获取数据:
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中的arraylist返回特定的对象(索引)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!