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
?
bookData[1].getName();
This is incorrect because the [1]
syntax only works for an array. It does not work with an ArrayList
.
for Arraylist you can use only
bookData.get(1).getName();
bookData[1].getName(); is used only with Array and Not with Arraylist
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.