java - Find name in Arraylist, then transfer contents to an array? -


i want find name in arraylist of students, transfer selection of 50 choices new array called mychoices (will later compared against others matches). student class contains name , arraylist of choices. here relevant loop:

int matches[] = new int[students.size()]; int mychoices[] = new int[students.get(0).getchoices().size()];  for(int = 0; < students.get(i).getchoices().size(); i++){     if(students.get(i).getname().equals("garrett m")){         mychoices[i] = students.get(i).getchoices().get(i);     } }  for(int = 0; < mychoices.length; i++){     system.out.println(mychoices[i]); } 

in last loop, i'm trying print choices, come out this:

0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0  0 0 0 0 0 0 0 0 0 

(that's not 50, gist of -- in output there's 49 zeros , 1 1.) actual output should begin 1 , mixture of 0,1, , -1:

1 -1 1 1 1 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 0 1 -1 0 1 0 0 0 0 1 1 1 1 0 1

any idea going wrong?

you using same index i both students list , students.get(i).getchoices() list. that's wrong.

you need nested loop:

for(int = 0; < students.size(); i++) { // iterate on students find 1                                            // having required name     if(students.get(i).getname().equals("garrett m")){         // iterate on choices of found student , collect them array         (int j = 0; j < students.get(i).getchoices().size; j++) {             mychoices[j] = students.get(i).getchoices().get(j);         }         break;     } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -