How can I return a specific object (index) from an

2019-09-20 04:28发布

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.

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);

so if I wanted to assign the created student (pat) the state of index 1 how would I do that?

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.

How do I force it to break the do as soon as "quit" is typed?

7条回答
老娘就宠你
2楼-- · 2019-09-20 04:52

There are two things you need to know:-

Firstly, you're using a do-while loop, which is an exit check loop, hence it will execute once atleast, before terminating. If you want to avoid it, try using a while loop.

Next, to retrieve all elements from the list, you can either use the list.get(index) or the for loop traversal as below:-

List<Student> students = new ArrayList<Student>();
for (Student student : students) {
    // Do something with the student object.
}

Update:-

The for you've seen above, is actually Foreach loop. This is what it means

for each item in collection:
  do something to item

Hence, the loop means,

for each item(student) in collection(students)
do something to item(student)

Now coming back to looping part. Forget everything, and just use a break;.

After this piece of code studentNumber=keybrd.next();, do a check and break the loop.

if(studentNumber.equals("quit")){
    break;
}

Simple solution.

查看更多
仙女界的扛把子
3楼-- · 2019-09-20 04:59

Use get(int)method to retrieve an element.

Use add(int, E)method to insert an element at an index.

查看更多
唯我独甜
4楼-- · 2019-09-20 05:07

Why not use a hashmap with key as Student number and Object to that key as Student Object. In this way you could avoid duplication as well.
For the sentinel

         // 1. Create a Scanner using the InputStream available.
        Scanner scanner = new Scanner( System.in );
        String input=null;
do{
        // 2. Don't forget to prompt the user
        System.out.print( "Type some data for the program: " );

        // 3. Use the Scanner to read a line of text from the user.
         input = scanner.nextLine();

        // 4. Now, you can do anything with the input string that you need to.
        // Like, output it to the user.
        System.out.println( "input = " + input );
}while(!input.equalsIgnoreCase("quit"));
查看更多
仙女界的扛把子
5楼-- · 2019-09-20 05:08

The ArrayList is not an array... It's a List which uses arrays as its backing store.

The retrieval method from list is list.get(index)

Alternately, you can use some syntactic sugar to just loop over them

List<Student> students = new ArrayList<Student>();
// add students
for (Student student : students) {
  // do something with student
}
查看更多
爷的心禁止访问
6楼-- · 2019-09-20 05:10

You can add a toString() method to your Students class :

  @Override
  public String toString()
  {
      return "\tStudentNumber: " + studentNumber
                + "\tCourse: " + course 
                + "\tMarks: " + marks;

  }

And then you can say :

   System.out.println(studentsCourses);

As far as this is concerned :

   How do I force it to break the do as soon as "quit" is typed?

This behaviour is because you are using do..while() ! Try using while instead.

查看更多
贼婆χ
7楼-- · 2019-09-20 05:11

"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());
    }

or

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);
查看更多
登录 后发表回答