-1
獲取ArrayIndexOutOfBoundException
同時插入元素到二維陣列..我不知道問題出在哪裏.. 我試了很多,但不明白爲什麼這是給異常。這是我的代碼。 .Thanks提前..ArrayOutOfBoundException雖然插入元素
public class TruthTableAutoMateClass extends JPanel implements ActionListener
{
static int size;
private int i = 0;
static JButton btnOk = new JButton();
String[][] array= new String[size][];
private double[][] dim = { {0.50, 0.50}, {0.25, 0.20, 0.20, 0.20, TableLayout.FILL}};
public static void main(String args[])
{
TruthTableAutoMateClass auto = new TruthTableAutoMateClass();
checkAdding(auto);
btnOk.addActionListener(auto);
}
private static String[] prepareArray()
{
String[] arr = new String[2];
if (size == 16)
{
String text1 = field1.getText();
String text2 = field2.getText();
String text3 = field3.getText();
String text4 = field4.getText();
String firstArray = new StringBuilder().append(text1).append("|").append(text3).append("|").append(text3).append("|").append(text4).toString();
arr[0] = firstArray;
arr[1] = ledValue1.getText();
}
else if (size == 9)
{
}
else if (size == 4)
{
}
return arr;
}
private static void checkAdding(TruthTableAutoMateClass auto1)
{
TruthTableAutoMateClass auto = auto1;
String answer = JOptionPane.showInputDialog("How many values are there 4 or 3 " + "if 4 yes else press no");
if (answer.equalsIgnoreCase("yes"))
{
size = 4 * 4;
auto.add(lbl1, "0,0");
auto.add(lbl2, "0,1");
auto.add(lbl3, "0,2");
auto.add(lbl4, "0,3");
auto.add(field1, "1,0");
auto.add(field2, "1,1");
auto.add(field3, "1,2");
auto.add(field4, "1,3");
auto.add(ledValue1, "1,4");
/*auto.add(ledValue2);
auto.add(ledValue3);
auto.add(ledValue4);*/
auto.add(btnOk, "0,4");
}
else
{
size = 3 * 3;
String answer2 = JOptionPane.showInputDialog("Again enter yes or no" + "3 for yes , 2 for no");
if (answer2.equalsIgnoreCase("yes"))
{
auto.add(lbl1, "0,0");
auto.add(lbl2, "0,1");
auto.add(lbl3, "0,2");
auto.add(field1, "1,0");
auto.add(field2, "1,1");
auto.add(field3, "1,2");
auto.add(ledValue1, "1,4");
/* auto.add(ledValue2);
auto.add(ledValue3);*/
auto.add(btnOk, "0,4");
}
else
{
size = 2 * 2;
auto.add(lbl1, "0,0");
auto.add(lbl2, "0,1");
auto.add(field1, "1,0");
auto.add(field2, "1,1");
auto.add(ledValue1, "1,4");
/* auto.add(ledValue2);*/
auto.add(btnOk, "0,4");
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
array[i] = prepareArray();
i++;
System.out.println(Arrays.toString(array));
}}
第一步是讀取堆棧跟蹤以及異常的文檔。不發佈堆棧跟蹤會迫使我們猜測,並且仔細檢查你的大部分不相關的代碼,而不是專注於問題出在哪裏。這是有價值的信息。不要忽視它。 –
@JBNizet ArrayOutofbound異常在我試圖訪問某些界限時出現,但在我的代碼中並未試圖訪問界限。請告訴我y? – JAVA
你讀過我以前的評論嗎?它不堅持讀取異常的堆棧跟蹤的重要性嗎?如果你沒有發佈它,我怎麼讀? –