Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayIndexOutOfBoundsException,字符串,Int_Java_String_Integer_Int_Row - Fatal编程技术网

Java ArrayIndexOutOfBoundsException,字符串,Int

Java ArrayIndexOutOfBoundsException,字符串,Int,java,string,integer,int,row,Java,String,Integer,Int,Row,我的eror in程序是 java.lang.ArrayIndexOutOfBoundsException at knapsacproject.Interface.jButton4ActionPerformed(Interface.java:221) 此代码用于按钮,它在jTextField4中打印结果,但不写入任何内容 我的java代码: DefaultTableModel model = (DefaultTableModel)jTable1.getModel(); int nRow =

我的eror in程序是

java.lang.ArrayIndexOutOfBoundsException
at knapsacproject.Interface.jButton4ActionPerformed(Interface.java:221)
此代码用于按钮,它在jTextField4中打印结果,但不写入任何内容

我的java代码:

 DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
 int nRow = model.getRowCount();
 try{
 algorithm algo = new algorithm(getTableData(model, 1),getTableData(model, 2),
Integer.parseInt(jTextField1.getText)),
Integer.parseInt(jTextField2.getText)),Integer.parseInt(jTextField3.getText()));
int[] result = algo.getResult();
for(int i = 0; i <nRow;i++) {
if(result[i]==1)
model.setValueAt("take", i , 3);
else
model.setValueAt("leave", i, 3);
}
jTable1.getColumnModel().getColumn(3).setCellRenderer(new StatusColumnCellRenderer());
jTextField4.setText(Integer.toString(result[nRow]));

有什么问题吗?

尝试使用
result[nRow-1]
而不是像这样使用
result[nRow]

 jTextField4.setText(Integer.toString(result[nRow-1]));

因为数组的索引从
0
开始到
nRow-1
而不是
nRow

请尝试使用
result[nRow-1]
而不是
result[nRow]
,如下所示:

 jTextField4.setText(Integer.toString(result[nRow-1]));

因为数组的索引从
0
开始到
nRow-1
而不是
nRow

result
数组大小是
nRow
,但是您试图使用索引
nRow
访问元素,而在java中,数组的起始索引是
0
,所以您无法这样做。我猜您需要将问题行更改为:

jTextField4.setText(Integer.toString(result[nRow-1]));

result
数组大小是
nRow
,但您正试图使用索引
nRow
访问元素,并且您无法这样做,因为在java中数组的起始索引是
0
。我猜您需要将问题行更改为:

jTextField4.setText(Integer.toString(result[nRow-1]));

谢谢,没事,谢谢,没事,谢谢,没事,没事,谢谢,没事