aspose file tools
The moose likes Java in General and the fly likes Is accessing ArrayList through Index consistent? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Is accessing ArrayList through Index consistent?" Watch "Is accessing ArrayList through Index consistent?" New topic
Author

Is accessing ArrayList through Index consistent?

Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
Hi Ranchers,
I have a very basic question. Imagine I have an arrayList that has Strings

Can the arraylist guarantee accessing it through index gives me the same result all the time? In the above example if I say arrStr.get(0). Does this give "First" always???
Thanks in advance,
Sham.


Raghusham Sankargal
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Yes. If you use that version of add method of class ArrayList you can be sure that your String First will be at index 0 (if the list was empty before the add)


Bye,
Nicola
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26192
    
  66

Sham,
Yes. Assuming nobody updates the first item. If I add a new item using the index "0", it will then be the first item.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is accessing ArrayList through Index consistent?
 
Similar Threads
Swap sublist of an arraylist with the sublist of another arraylist
arraylist
Undetectable error in code
Java Collections
LinkedList/ArrayList: need a hybrid