2010-12-21 122 views
0

因此,我正在處理的項目需要我有一維數組的二維陣列列表。但每次我試圖在我的數據加載時間,我得到一個錯誤:java中的二維陣列列表

不能這樣做,因爲錯誤的輸入 java.lang.IndexOutOfBoundsException這個opperation:指標:1,大小:0

的一些投入。我不知道我在哪裏出錯了。請幫助一下?

源代碼:

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 
import java.io.InputStream; 


public class Facebull 
{ 


    public static void main (String[] args) { 

     if(args.length != 0){ 
      load(args[0]); 

     } 
     else{ 
      load("testFile"); 
     } 

    } 

    public static void load(String fname) { 
     int costOfMach = 0; 
     ArrayList <Integer> finalMach = new ArrayList<Integer>(); 
     ArrayList <ArrayList<int[]>>machines = new ArrayList<ArrayList<int[]>>(); 

     Scanner inputFile = null; 

     File f = new File(fname); 

     if (f.exists()) 
     { 

      try 
      { 
       inputFile = new Scanner (f); 

      } 

      catch (FileNotFoundException e) 
      { 
       JOptionPane.showMessageDialog(null,"Can't find the file\n" + e); 
      } 

      int i = 0; 


      while (inputFile.hasNext ()) 
      { 

       String str = inputFile.nextLine (); 

       String [ ] fields = str.split ("[\t ]"); 

       System.out.println(str); 

       if (!(fields[0].isEmpty() || fields[0].equals (""))){ 

        fields[0] = fields[0].substring(1); 
        fields[1] = fields[1].substring(1); 
        fields[2] = fields[2].substring(1); 

        try 
        { 
         //data to be inputed is 0 and 3 location of data is 1 and 2 
         int[] item = new int[2]; 
         item[1] = Integer.parseInt(fields[0]); 
         item[0] = Integer.parseInt(fields[3]); 

         if(machines.size() < Integer.parseInt(fields[1])){ 
          ArrayList<int[]> column = new ArrayList<int[]>(); 
          machines.add (Integer.parseInt(fields[1])-1, column); 
          System.out.println("we're in the if"); 
         } 
         machines.get(Integer.parseInt(fields[1])-1).add(Integer.parseInt(fields[2])-1, item); 

        } 
        //catches any exception 
        catch (Exception e) 
        { 
         System.out.println("Can't do this opperation because of bad input \n" + e); 
        } 

       } 

      } 
      inputFile.close (); 
     } 
     System.out.print(machines); 

    }//end load 
} 
+0

拋出異常在哪裏?代碼中可能存在各種各樣的地方。 – tddmonkey 2010-12-21 20:36:58

+0

this:machines.get(Integer.parseInt(fields [1]) - 1).add(Integer.parseInt(fields [2]) - 1,item); 是拋出異常的代碼。 – 2010-12-21 20:44:53

回答

0

這裏:

machines.add(Integer.parseInt(fields[1])-1, column); 

您沒有指定大小的機器ArrayList的任何地方。 ArrayList會在您使用add的單參數版本時動態增長,因爲它會添加到列表的末尾,但是在使用雙參數版本時,ArrayList必須至少與您指定的索引一樣長。由於您沒有指定大小,並且您沒有在任何地方使用單參數添加,所以ArrayList的大小爲0,因此無論您提供哪個索引,它都會超出範圍。您可以在創建ArrayList實例時指定大小,也可以在調用add之前使用ArrayList.ensureCapacity。

然後,你有同樣的問題與get的行,試圖對一個空的ArrayList使用雙參數添加。

0

聲明如下所示用於2D的ArrayList該ArrayList:

ArrayList<ArrayList<String>> my2DarrayList = new ArrayList<ArrayList<String>>(); 

現在在索引0處添加字符串的一個新的ArrayList:

my2DarrayList.add(new ArrayList<String>()); 

現在,作爲新的ArrayList在索引0,所以輸入您必須將如下所示的值添加到特定的ArrayList:

my2DarrayList.get(0).add("String is added here");