我們還沒有涉及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);
}
}
如果有人能指引我正確的方向,那將是非常有價值的。我試圖調試,但我可以得到任何有用的東西。
在此先感謝。
這對於數組也不起作用。我不認爲你掌握了對象或方法如何工作,或者變量範圍是什麼。除此之外,上面的*代碼不會拋出異常,因爲size()會返回'0' – 2012-03-07 18:32:19
好的,那麼我該如何去做這件事?記住,我只是在學習每個人都犯錯誤。 – Kyle93 2012-03-07 18:34:06