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:
- [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:
- Oscar 2. Justin 3. Austin 4. Jacob ...