0

I just used the ArrayList <ArrayList<String>> list = new ArrayList<ArrayList<String>>(); method for the first time and the output wasn't exactly in the way I wanted it to be.

I'm trying to make lists of different students and sorting them by what grade they're in. But, I am also trying to have a list that displays all the students, regardless of their grade. So this is the code that I was using:

ArrayList <ArrayList<String>> allStudents = new ArrayList<ArrayList<String>>();
ArrayList <String> gradeNines = new ArrayList();
ArrayList <String> gradeTens = new ArrayList();
ArrayList <String> gradeElevens = new ArrayList();
ArrayList <String> gradeTwelves = new ArrayList();

boolean firstSelection = true;

public void grade(String a, String b, ArrayList c)
{

    a = jComboBox1.getSelectedItem() + "";

    if (a.equals(b))
    {
        studentOutput.setText("");
        int x = 0; 
        for (int indexNum = 0; indexNum < c.size(); indexNum++) 
        { 
             x = indexNum + 1; 
             studentOutput.append(num + ". " + c.get(indexNum) + "\n"); 
        }
    }
}   

 private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

    String studentGrade = jComboBox1.getSelectedItem() + "";

    String all = "All Grades";
    String nine = "Grade Nine";
    String ten = "Grade Ten";
    String eleven = "Grade Eleven";
    String twelve = "Grade Twelve";

    if (firstSelection)
    {
         Collections.addAll(allStudents, gradeNines, gradeTens, gradeElevens, gradeTwelves);
         Collections.addAll(gradeNines, "Oscar", "Justin",....);
         Collections.addAll(gradeTens, "Austin", "Jacob", "Evie"....);
         Collections.addAll(gradeElevens, "Olivia", "Elizabeth"...);
         Collections.addAll(gradeTwelves, "Ryan", "Jade"...);
         firstSelection = false;
    }

    grade(studentGrade, all, allStudents);
    grade(studentGrade, nine, gradeNines);
    grade(studentGrade, ten, gradeTens);
    grade(studentGrade, eleven, gradeElevens);
    grade(studentGrade, twelve, gradeTwelves);

But the way this outputted, it was like:

  1. [Oscar, Justin] 2. [Austin, Jacob, Evie] 3. [Olivia, Elizabeth] 4. [Ryan, Jade]

Is there a way to make it so that it outputs so that it shows each name individually like:

  1. Oscar 2. Justin 3. Austin 4. Jacob ...

1 Answers1

0

Firstly, these type of "grouping" problems are better handled using a Map (HashMap or an ordered implementation). You could have the grades as the key and the list of student names as the values. That would feel as a better and easier way to handle collection data than a nested ArrayList.

Regarding your question,

Is there a way to make it so that it outputs so that it shows each name individually

No. These are separate ArrayLists. You will need to add/display them together, explicitly.

Further, I suggest you use the groupingBy function of Streams in Java 8. That will fit your use case very well.

With Streams, you could choose to display all items together OR group them by grades and display based on the chosen crteria(grade).

See this SO answer: https://stackoverflow.com/a/30202075/599851

iCrus
  • 1,210
  • 16
  • 30