【发布时间】:2016-03-08 17:31:40
【问题描述】:
这个问题已经面临很多时间了,我知道,我遇到了这些问题,但无法理解我的问题。这是我下面给出的代码的一部分,
public ArrayList<Slice> network_slices;
public ArrayList<ArrayList<String>> nfs_slices= new ArrayList<ArrayList<String>>();
public int index;
private class TabSelect implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JTabbedPane source = (JTabbedPane) e.getSource();
if (source.getSelectedComponent() != null) {
index = source.getSelectedIndex();
}
}
}
private int count = 0;
private class AddNetworkFuncitons implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String buttonselected = (String) e.getActionCommand();
JButton button = new JButton(buttonselected);
network_slices.get(index).add(button).setVisible(true);
int c =nf_list.indexOf(buttonselected);
System.out.println(index);
System.out.println(c);
nfs_slices.get(index).set(c,nf_list.get(c));
System.out.println(nfs_slices);
// System.out.println(nfs_slices.get(index));
count ++;
}
}
每当执行该操作时,它应该将buttonselected 字符串添加到与index 列表对应的arraylist(因为有二维ArrayList)。 changeListener 随时更改索引。
其中nf_list 是一个包含 4 个元素的字符串的 ArrayList。
但它抛出异常。
我在构造函数中初始化了network_slicesnetwork_slices = new ArrayList<Slice>();
【问题讨论】:
-
在哪里声明了
nf_list,你在哪里初始化了network_slices?发布完整的代码和异常跟踪。 -
我在问题中进行了编辑。
-
您正在将索引存储在状态中。当您将 network_slices 设置为新的 ArrayList 时,它的新大小为零。因此,如果其中没有任何元素,任何对 get(index) 的调用都会失败。您需要添加一个检查以确保您的 network_slices ArrayList 不为空。
-
你在谈论 nfs_slices,因为当 network_slices 调用 get(index).它可以正常工作,我认为 nfs_slices 不能调用 get (index)。
标签: java arrays arraylist multidimensional-array