首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java java.lang.ArrayIndexOutOfBoundsException: 3错误消息

Java java.lang.ArrayIndexOutOfBoundsException: 3错误消息
EN

Stack Overflow用户
提问于 2015-06-13 06:43:46
回答 3查看 5.1K关注 0票数 2

所以,除了在测试showSortedRows()方法时,我的代码还在工作。它向我展示了我想要的输出,但同时也给了我这个错误消息。我真的不知道为什么,请帮帮我!

错误消息:线程“主”java.lang.ArrayIndexOutOfBoundsException: 3中的异常 在Driver0.howSortedRows(Driver0.java:90) 选择(Driver0.java:35)在Driver0.main(Driver0.java:21)

代码语言:javascript
复制
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();
                    }     

                 }

              }
EN

回答 3

Stack Overflow用户

发布于 2015-06-13 06:46:20

循环一直运行到cl < dimension2,这样array[rw][cl+1]就会导致错误。

您必须将循环更改为for (int cl = 0; cl < dimension2-1; cl++)

票数 0
EN

Stack Overflow用户

发布于 2015-06-13 06:46:28

这句话是:

代码语言:javascript
复制
if (array[rw][cl] <= array[rw][cl+1])

当cl为dimension2-1时,cl +1将导致ArrayIndexOutOfBoundsException

顺便问一下,您确定double for循环中的rw++是正确的吗?

它应该是if条件的一部分吗?

当dimension2是> dimension1时,它也会导致ArrayIndexOutOfBoundsException

showSortedRows() 应该是:

代码语言:javascript
复制
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());
        }
    }
}

输出:

代码语言:javascript
复制
Row 1 is sorted!
Row contents: 2 3 4 5 10 
Row 3 is sorted!
Row contents: -3 -1 0 1 5 
票数 0
EN

Stack Overflow用户

发布于 2015-06-13 06:55:24

发生这种情况的原因是:

代码语言:javascript
复制
 if (array[rw][cl] <= array[rw][cl+1])  

所发生的情况是,当变量cl在内循环的最后一次迭代中变为dimension2 - 1时,它会检查[cl][dimension2]第四次索引。它不存在于数组中。您的数组的大小为[row][dimension2],这意味着它从0row - 1计数行,从0计数到dimension2 - 1计数列。尽管如此,它仍在尝试访问dimension2第四大索引。因此,它给出了ArrayIndexOutOfBoundsException

解决办法:

您的循环必须是:

代码语言:javascript
复制
for(int cl = 0; cl < dimension2 - 1; cl ++)  

希望这会有所帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30815864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档