簡體   English   中英

Java-從另一個類的ArrayList讀取

[英]Java - Reading from an ArrayList from another class

我們沒有只介紹ArrayLists數組和2D數組。 我需要做的是能夠從另一個類的ArrayList中讀取。 主要目的是在for循環中從它們讀取並使用存儲在其中的值顯示項目。 但是,我已經制作了這個快速程序來對其進行測試並不斷得到此錯誤

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個元素的ArrayList位置1進行了索引。 注意循環索引變量x起始位置。 但是請考慮以下這一行:

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

xcoords指向一個新的空ArrayList ,而不是您在Objects類中創建的一個空列表。 要獲取 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));
    }
}

在這里,您只是創建兩個完全不相關的列表。 使數組列表成為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

您有一個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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM