Java - Reading from an ArrayList from another class

后端 未结 3 704
梦谈多话
梦谈多话 2020-12-20 06:00

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 fo

相关标签:
3条回答
  • 2020-12-20 06:16

    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.

    0 讨论(0)
  • 2020-12-20 06:32

    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));
        }
    }
    
    0 讨论(0)
  • 2020-12-20 06:40

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题