所以我的代码工作正常,除非我在测试方法showSortedRows()
时。它向我展示了我想要的输出,但同时给了我这个错误信息。我真的不知道为什么,请帮助我!
错误消息:线程" main"中的异常 java.lang.ArrayIndexOutOfBoundsException:3
在 Driver0.showSortedRows(Driver0.java:90)
在 Driver0.choice(Driver0.java:35)在Driver0.main(Driver0.java:21)
import java.io.*;
import java.util.*;
public class Driver0
{
public static int [][] array;
public static int dimension1, dimension2;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Project 0.");
System.out.println("What is the name of the data file? ");
System.out.print("> ");
String file = input.nextLine();
readFile(file);
String nextCommand = "";
while (!(nextCommand.equals("quit")))
{
System.out.println("\nNext command");
System.out.print("> ");
nextCommand = input.nextLine();
choice (nextCommand);
}
}
public static void choice(String command)
{
switch (command)
{
case "help": System.out.println("show array\nwhich rows sorted\nwhich cols sorted"+
"increase row i by n\nincrease col j by n\nquit\n");
break;
case "show array": showArray();
break;
case "which rows sorted": showSortedRows();
break;
/*case "which cols sorted": showSortedCols();
case "increase row i by n": increaseRow();
case "increase col j by n": increaseCol();*/
}
}
public static void readFile(String file)
{
try
{
Scanner sc = new Scanner(new File(file));
dimension1 = sc.nextInt();
dimension2 = sc.nextInt();
array = new int[dimension1][dimension2];
while (sc.hasNext())
{
for (int row = 0; row < dimension1; row++)
{
for (int column = 0; column < dimension2; column++)
{
array[row][column] = sc.nextInt();
}
}
}
sc.close();
}
catch(Exception e)
{
System.out.println("Error: file not found or insufficient requirements.");
}
}
public static void showArray()
{
for (int rows = 0; rows < dimension1; rows++)
{
for (int col = 0; col < dimension2; col++)
{
System.out.printf("%2d ", array[rows][col]);
}
System.out.println();
}
}
public static void showSortedRows()
{
for (int rw = 0; rw < dimension1; rw++)
{
for (int cl = 0; cl < dimension2; cl++)
{
if (array[rw][cl] <= array[rw][cl+1])
System.out.printf("%2d,", rw);
rw++;
}
System.out.println();
}
}
}
答案 0 :(得分:0)
您的循环会一直运行到cl < dimension2
,因此array[rw][cl+1]
会导致错误。
您必须将循环更改为for (int cl = 0; cl < dimension2-1; cl++)
答案 1 :(得分:0)
这是这一行:
if (array[rw][cl] <= array[rw][cl+1])
当cl为dimension2-1
时,cl + 1会导致ArrayIndexOutOfBoundsException
。
顺便问一下,你确定double for循环中的rw++
是否正确?
它应该是if
条件的一部分吗?
当dimension2> dimension1,它也可以导致ArrayIndexOutOfBoundsException
。
showSortedRows()
应为:
public static void showSortedRows() {
for (int rw = 0; rw < dimension1; rw++) {
boolean sortedRow = true;
StringBuilder sb = new StringBuilder();
for (int cl = 0; cl < (dimension2 - 1); cl++) {
sb.append(array[rw][cl]).append(" ");
if (array[rw][cl] > array[rw][cl + 1]) {
sortedRow = false;
}
if((cl + 1) == (dimension2 - 1)) {
sb.append(array[rw][cl + 1]).append(" ");
}
}
if(sortedRow) {
System.out.println("Row " + (rw + 1) + " is sorted!");
System.out.println("Row contents: " + sb.toString());
}
}
}
<强>输出:强>
Row 1 is sorted!
Row contents: 2 3 4 5 10
Row 3 is sorted!
Row contents: -3 -1 0 1 5
答案 2 :(得分:0)
正是因为这条线而发生:
if (array[rw][cl] <= array[rw][cl+1])
发生的事情是,当你的变量cl
在内部循环的最后一次迭代中变得等于dimension2 - 1
时,它会检查[cl][dimension2]
索引。这在数组中不存在。您的数组的大小为[row][dimension2]
,这意味着对于行,0
到row - 1
的行数为0
到dimension2 - 1
。它仍在尝试访问dimension2
索引。因此它会给你ArrayIndexOutOfBoundsException
。
你的循环必须是:
for(int cl = 0; cl < dimension2 - 1; cl ++)
希望这会有所帮助:)