java 数组列表的java数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7138825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java array of arraylists
提问by mbreen
I have four columns of buttons in my program. Buttons move between columns when assigned new ones. Instead of declaring 4 separate arraylists to store the buttons, is there a way to create 1 array of arraylists so I can simply itterate through the array?
我的程序中有四列按钮。分配新按钮时,按钮会在列之间移动。有没有办法创建 1 个数组列表,而不是声明 4 个单独的数组列表来存储按钮,这样我就可以简单地遍历数组?
I've tried List<JButton>[] lists = new ArrayList<JButton>[5];
我试过了 List<JButton>[] lists = new ArrayList<JButton>[5];
But that won't 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 currently.Once a new column is selected it checks to see which listthe button was in and removes it, then adds it to the new list. But my new problem is that using lists[i] will no longer work. Idk how to properly loop through my list of arraylists using this declaration:
这是我目前的代码。选择新列后,它会检查按钮所在的列表并将其删除,然后将其添加到新列表中。但我的新问题是使用列表 [i] 将不再有效。我知道如何使用此声明正确循环遍历我的数组列表:
List<ArrayList<JButton>> lists = new ArrayList<ArrayList<JButton>>();
采纳答案by dertkw
You have to keep a list of lists of JButton objects:
您必须保留 JButton 对象列表的列表:
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());
}
}
回答by Parasou
Giving an example of what worked for me / talked above.
举一个对我有用/上面谈到的例子。
List []oArrayOfArrayList = new ArrayList[2];
List<String> oStringList = new ArrayList<String>();
List<Integer> oIntegerList = new ArrayList<Integer>();
oArrayOfArrayList[0] = oStringList ;
oArrayOfArrayList[1] = oIntegerList ;
回答by Jeremy
You cannot create arrays from classes with generic parameters.
您不能从具有泛型参数的类创建数组。
You'll either want to make a list of a list, or forgo generic parameters.
您要么想要制作一个列表列表,要么放弃通用参数。
回答by DwB
You are not able to call the new operator for a generic object because at run time the type has been erased.
您无法为通用对象调用 new 运算符,因为在运行时该类型已被删除。
回答by hundreAd
create a class for your ArrayList:
为您的 ArrayList 创建一个类:
private class xxx extends ArrayList<JButton> {
// need serial to keep IDE happy
private static final long serialVersionUID = -2392107594926751233L;
};
private xxx[] lists = new xxx[5];