如何从Java中的arraylist返回特定的对象(索引)?

编程入门 行业动态 更新时间:2024-10-17 15:23:29
本文介绍了如何从Java中的arraylist返回特定的对象(索引)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个名为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返回特定的对象(索引)?

本文发布于:2023-11-06 23:52:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   对象   Java   arraylist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!