Iterating over two arrays simultaneously using for each loop in Java

前端 未结 4 460
再見小時候
再見小時候 2020-12-16 05:45

Student\'s names(String[]) and corresponding marks(int[]) are stored in different arrays.

How may I iterate over both arrays together using for each loop in Java ? <

4条回答
  •  囚心锁ツ
    2020-12-16 06:21

    The underlying problem is actually that you should tie both of the arrays together and iterate across just one array.

    Here is a VERY simplistic demonstration - you should use getters and setters and you should also use a List instead of an array but this demonstrates the point:

    class Student {
      String name;
      int mark;
    }
    Student[] students = new Student[10];
    
    for (Student s : students) {
      ...
    }
    

提交回复
热议问题