2012-03-07 165 views
1

我們還沒有涉及ArrayLists僅數組和二維數組。我需要做的是能夠從另一個類的ArrayList中讀取數據。主要目標是在for循環中讀取它們並使用存儲在其中的值來顯示項目。但是,我做了這個快速程序來對其進行測試,並不斷收到此錯誤Java - 從另一個類的ArrayList讀取

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.rangeCheck(ArrayList.java:604) 
    at java.util.ArrayList.get(ArrayList.java:382) 
    at Main.Main(Main.java:14) 

這裏是我的代碼

import java.util.ArrayList; 

public class Main 
{ 
    public static void Main() 
    { 
     System.out.println("Test"); 
     ArrayList <Objects> xcoords = new ArrayList<Objects>(); 

     for(int x = 1 ; x < xcoords.size() ; x++) 
     { 
      System.out.println(xcoords.get(x)); 
     } 
    } 
} 

然後是類,其中的ArrayList是

import java.util.ArrayList; 

public class Objects 
{ 
    public void xco() 
    { 
     ArrayList xcoords = new ArrayList(); 
     //X coords 
     //Destroyable 
     xcoords.add(5); 
     xcoords.add(25); 
     xcoords.add(5); 
     xcoords.add(5); 
     xcoords.add(25); 
     xcoords.add(5); 
     //Static Walls 
     xcoords.add(600); 
     xcoords.add(400); 
     xcoords.add(600); 
    } 
} 

如果有人能指引我正確的方向,那將是非常有價值的。我試圖調試,但我可以得到任何有用的東西。

在此先感謝。

+0

這對於數組也不起作用。我不認爲你掌握了對象或方法如何工作,或者變量範圍是什麼。除此之外,上面的*代碼不會拋出異常,因爲size()會返回'0' – 2012-03-07 18:32:19

+0

好的,那麼我該如何去做這件事?記住,我只是在學習每個人都犯錯誤。 – Kyle93 2012-03-07 18:34:06

回答

6

嚴格來說,例外情況是由於索引0個元素的ArrayList的位置1。注意你在哪裏啓動你的循環索引變量x。但考慮這條線:

ArrayList <Objects> xcoords = new ArrayList<Objects>(); 

xcoords指向一個新的空ArrayList,沒有你在課堂上的對象創建的。爲了得到ArrayList,改變方法xco

public ArrayList<Integer> xco() { // make sure to parameterize the ArrayList 
    ArrayList<Integer> xcoords = new ArrayList<Integer>(); 

    // .. add all the elements .. 

    return xcoords; 
} 

然後,在你的main方法

public static void main(String [] args) { // add correct arguments 

    //.. 
    ArrayList <Integer> xcoords = (new Objects()).xco(); 

    for(int x = 0 ; x < xcoords.size() ; x++) { // start from index 0 
     System.out.println(xcoords.get(x)); 
    } 
} 
+0

非常感謝,當你說這樣的話時, 。我會試着用其他一些方法來確保我完全掌握它,但是再次感謝! – Kyle93 2012-03-07 18:45:01

3

在這裏,您只需創建兩個完全無關的列表。數組列表可以是Objects類的屬性,並通過實例方法檢索它,或者從實例或靜態方法返回它,或者使屬性爲靜態。在大多數情況下,IMO的前兩項是可取的。

public class Objects { 
    public static List<Integer> getXcoords() { 
     List<Integer> xcoords = new ArrayList<Integer>(); 

     // Your same code, but adding: 
     return xoords; 
    } 
} 

然後使用它:

import java.util.ArrayList; 

public class Main { 
    // Note the lower-case "main" here. You want that. 
    public static void main() { 
     List<Integer> xcoords = Objects.getXcoords(); 
     // etc. 

而且,你的List應該是Integer,不Objects,這將創建一個集合持有的Objects實例。您可能需要退後一步並以更好的方式將列表與數組關聯起來 - 您不會創建一個數組Objects,對嗎?不,你會有一組intInteger。另外,還有Arrays.asList

+0

@BrianRoach是的,已經做了 - 粘貼很多,沒有足夠的剪切。 – 2012-03-07 18:39:02

+0

非常感謝你,你和佩斯利在那裏都非常有幫助,但是他用我理解得更輕鬆的方式解釋了它。雖然你最後一個關於Array對象的陳述非常好。再次感謝你!另外,如果我可以'+'發表我的意見,但是我可以由於代表:/當我可以的時候,再次感謝 – Kyle93 2012-03-07 18:48:24

1

你有一個IndexOutOfBoundsException這意味着你正在嘗試在一個數組來訪問一個元素,其不存在。

但是在這裏發佈的代碼中,根本沒有訪問數組(您的for循環將不會執行一次,因爲列表爲空),這意味着您的異常將被引發到別的地方。

但是你的代碼也沒有任何意義。我儘可能地將它重構爲儘可能接近您的代碼,因此您可以看到它是如何工作的:

public static void main(String[] args){ 
    Objects myObjects = new Objects(); 
    ArrayList<Integer> listFromMyObjects = myObjects.getList(); 
    for(int x = 0 ; x < listFromMyObjects.size() ; x++) 
    { 
     System.out.println(listFromMyObjects.get(x)); 
    } 
} 


public class Objects 
{ 
    private ArrayList<Integer> myList; 

    public Objects(){ 
     myList = new ArrayList<Integer>(); 
     myList.add(5); 
     myList.add(25); 
     myList.add(5); 
     myList.add(5); 
     myList.add(25); 
     myList.add(5); 
     myList.add(600); 
     myList.add(400); 
     myList.add(600); 
    } 

    public ArrayList<Integer> getList(){ 
     return myList; 
    } 
} 
+0

感謝您的回答!再次與其他答案我沒有更好的理解,並可以看到我出錯的原因。現在有很大的意義它已被解釋,謝謝! – Kyle93 2012-03-07 18:51:22

相關問題