(Java)无法修复数组越界错误的2D数组

时间:2014-02-14 02:30:11

标签: java arrays multidimensional-array indexoutofboundsexception

我一直收到此java.lang.ArrayIndexOutOfBoundsException: 5错误但不知道原因。我试图从文本文件中将数字存储到我的2D数组中,但它没有正确存储数据。此外,我不允许使用任何预定义的java方法,例如“。length”实例字段,因为我将数据存储到2D数组中。基本上我有一个与驱动文件交互的类文件。

以下是名为“numbers”的文字文件:

3
5
8
2
4
1
7
9
11
14

这是包含方法的类文件:

public class array_class2D//class header
{
 //private instance variables

  final private int rows = 4;
  final private int columns = 5;
  public final int maxsize = 20;

  int r = 0;
  int c = 0;
  private int position_r = 1;
  private int position_c = 2;

  int[][] c_array = new int[rows][columns];

  public void arrayPrint2D()
  {
   System.out.println("Printing out the Complete array: ");

   for(int r = 0; r < rows; r++)
   {
     for(int c = 0; c < columns; c++)
     {
        //if( count2 < count || c_array[r][c] != 0 )
        //{  
           System.out.print(c_array[r][c] + " ");
          // count2++;
        //}
     }

     System.out.println();
   }
  }


  public void store2D(int num)
  {
    System.out.println("R: " + r);
    System.out.println("C: " + c);
    c_array[r][c] = num;

    if(r == 0 && c < columns)
    {
     c++;

    }
    else if(r == 0 && c == columns)
    {
     r++;
     c = 0; 
    }
    else if(r == 1 && c < columns)
    {
     c++;
    }
    else if(r == 1 && c == columns)
    {
     r++;
     c = 0;  
    }
    else if(r == 2 && c < columns)
    {
     c++;
    }
    else if(r == 2 && c == columns)
    {
     r++;
     c = 0;
    }
    else if(r == 3 && c < columns)
    {
     c++; 
    }
    else if(r == 3 && c == columns)
    {
     r++;
     c = 0;
    }
    else if(r == 4 && c < columns)
    {
     c++;
    }
    else
    {
     System.out.println(" Its Done. ");
    }
  }      


}

这是我的驱动程序文件:

import java.io.*;
import java.util.Scanner;

public class array2D_Driver
{
  public static void main(String[] args) throws IOException
  {
  //Intance variables  
  int num = 0;//used to store elements from txt file
  int input;//used to store user input
  int number = 0;//used in while loop below to test user input



  //object to use methods 
  array_class2D object = new array_class2D();


  //Scanner object to read keyboard input
  Scanner keyboard = new Scanner(System.in);

  //Opens the file
  File file = new File("numbers.txt");
  Scanner inputFile = new Scanner(file);



  System.out.println("***************Array Manipulation Program****************");

  System.out.println("\n\n");

  //Calling print method to show arrat is empty
  object.arrayPrint2D();
  System.out.println();//Buffer line

  //Reads the file elements into the array
  while (inputFile.hasNext())
  {
     num = inputFile.nextInt();//Basically each line on the text file has one element that is stored into the 'num' variable.
     object.store2D(num);//The num variable is then passed as a integer through the store method's parameters
  }
  object.arrayPrint2D();

 }
}//End of main method

提前谢谢!

2 个答案:

答案 0 :(得分:1)

您的if-else签入方法store2D()不是必需的,而且太乱了。改变它:

public void store2D(int num){
    System.out.println("R: " + r);
    System.out.println("C: " + c);
    c_array[r][c++] = num;
    if(c==5){
        c=0;
        r++;
    }
}

多数民众赞成。

答案 1 :(得分:0)

我认为这是你的问题

  c_array[r][c] = num;

当循环到达columns5变量public void store2D(int num)时,正在调用此方法,因为可能存在的数据多于数组维度,为避免这种情况,请尝试给出数组维度动态地基于该文件中的数据。