ugCode
ugCode

Reputation: 29

access array from arraylist

I have the following arraylist

ArrayList<AnotherClass> exmapleName = new ArrayList<AnotherClass>();
// AnotherClass is a class I created, I store objects of the class in this array, the code to this doesn't matter this works.

Inside of exampleName I have different types of data

public AnotherClass (String someString,  int someInt, int anotherInt, int[] intArray, ArrayList<Integer> arrayList)
//Header of the constructor

Now what I need to do is access and store arrayList[0]. But I need to do it once the object has been created so there is actually something inside arrayList.

This is what I've tried but doesn't seem to work

    public static void main(String[] args) {
    TestClass obj = new TestClass();
    obj.read(); //reads file
    testMethod();

}

       public void testMethod(){
         AnotherClass test1 = exampleName.get(1);
        ArrayList<Integer> test2 = test1.arrayList;
        int test3 = test2.arrayList[0];

} // I broke it down into separate lines to make it more understandable

compiler errors are as follows

cannot find symbol
                    int test3 = test2.arrayList[0];
symbol:   variable arrayList
location: variable test2 of type ArrayList<Integer>

1 error

Upvotes: 0

Views: 1021

Answers (1)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Change

int test3 = test2.arrayList[0];

to

int test3 = test2.get(0);

You use brackets to access arrays' elements, & the .get method of an ArrayList to access its elements.

Upvotes: 1

Related Questions