MR ROBOT
MR ROBOT

Reputation: 9

Error while Calling methods for objects stored inside a ArrayList

I have declared an ArrayList to store my objects of the class Books.

static ArrayList<Books> bookData = new ArrayList<Books>();

So I have multiple methods inside class Books. Let's say I have a method named... getName(), Let's assume I have objects stored inside ArrayList and I want to call the method for the object stored in the array list.

So Is this correct:

bookData[1].getName();

or

Is this correct:

bookData.get(1).getName();

If the first is wrong, then why? Is there a method to call the object stored inside ArrayList?

Upvotes: 0

Views: 50

Answers (2)

vishal s.
vishal s.

Reputation: 380

for Arraylist you can use only

bookData.get(1).getName();

bookData[1].getName(); is used only with Array and Not with Arraylist

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83607

bookData[1].getName();

This is incorrect because the [1] syntax only works for an array. It does not work with an ArrayList.

Upvotes: 4

Related Questions