简体   繁体   中英

Java - Reading from an ArrayList from another class

We've not covered ArrayLists only Arrays and 2D arrays. What I need to do is be able to read from an ArrayList from another class. The main aim is to read from them in a for loop and use the values stored in them to display items. However, I have made this quick program to test it out and keep getting this error

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)

Here is my code

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));
        }
    }
}

And then the class where the ArrayList is

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);
    }
}

If someone can point me in the correct direction it would be so valuable. I've tried to debug however I can get anything helpful.

Thanks in advance.

Strictly speaking, the exception is due to indexing location 1 of an ArrayList with 0 elements. Notice where you start you for loop index variable x . But consider this line:

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

xcoords points to a new, empty ArrayList , not the one you created in class Objects. To get that ArrayList , change the method xco like

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

    // .. add all the elements ..

    return xcoords;
}

then, in your main method

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));
    }
}

Here you're simply creating two completely unrelated lists. Either have the array list be a property of the Objects class and retrieve it through an instance method, or return it from an instance or static method, or make the property static. IMO the first two are preferable in most situations.

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

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

Then to use it:

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.

Also, your List should be of Integer , not of Objects , which would create a collection holding instances of Objects . You may want to take a step back and relate lists to arrays in a better way--you wouldn't create an array of Objects , would you? No, you'd have an array of int or Integer .

Also, there's Arrays.asList .

You have an IndexOutOfBoundsException which means that you are trying to access an element in an array which is not existing.

But in your code posted here you are not accessing an array at all (your for loop will not execute once because the list is empty), which means that your exception is thrown somewhere else.

But also your code doesn't make any sense. I refactored it for you while staying as close to your code as possible, so you can see how it could work:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM