Java arraylists array

I have four columns of buttons in my program. Buttons move between columns when assigning new ones. Instead of declaring 4 separate arraylists to store the buttons, is there a way to create 1 arraylists array so that I can just iterate through the array?

I tried List<JButton>[] lists = new ArrayList<JButton>[5];

But that will not work. What am I missing?

EDIT:

 for(int i = 0; i < 5; i++){ if(choiceList.getSelectedIndex() == i){ if(btnPersons[nameList.getSelectedIndex()].getX() == column[i]){ JOptionPane.showMessageDialog(null, "Error - Name already present in the column.","", 1); }else{ for(int j = 0; j < 5; j++){ if(lists[j].get(i) != null){ lists[j].remove(btnPersons[nameList.getSelectedIndex()]); } } lists[i].add(btnPersons[nameList.getSelectedIndex()]); lists[i].get(i).setBounds(column[i], ROWS[i], 125, 20); //reloadLocations(); } } } 

This is my code now. When a new column is selected, it checks which row the button is in, and deletes it, and then adds it to the new list. But my new problem is that using lists [i] will no longer work. Idk, how to scroll my list of my arraists correctly using this ad:

 List<ArrayList<JButton>> lists = new ArrayList<ArrayList<JButton>>(); 
+7
source share
4 answers

You must save the list of JButton object lists:

 List<List<JButton>> lists = new ArrayList<List<JButton>>(); // populate (replace with your code) lists.add(Arrays.asList(new JButton("list 1, button 1"), new JButton("list 1, button 2"))); lists.add(Arrays.asList(new JButton("list 2, button 3"), new JButton("list 2, button 4"))); lists.add(Arrays.asList(new JButton("list 3, button 5"), new JButton("list 3, button 6"))); lists.add(Arrays.asList(new JButton("list 4, button 7"), new JButton("list 4, button 8"))); // iterate for(List<JButton> subList : lists) { for(JButton button : subList) { System.out.println(button.getText()); } } 
+5
source

We give an example of what worked for me / was said above.

  List []oArrayOfArrayList = new ArrayList[2]; List<String> oStringList = new ArrayList<String>(); List<Integer> oIntegerList = new ArrayList<Integer>(); oArrayOfArrayList[0] = oStringList ; oArrayOfArrayList[1] = oIntegerList ; 
+4
source

You cannot create arrays from classes with common parameters.

You want to either make a list of the list or abandon the general parameters.

+3
source

You cannot call a new operator for a shared object because the type was deleted at run time.

+2
source

All Articles