• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList does it maintain order

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,
I have a bunch of strings that I am getting in an arrayList
These are actually primary key field values of a certain table

I then go and get the value from the table and store it in another ArrayList
I return both the arrayLists to another program

My doubt / query is - will the arraylist maintain the order exactly as I have entered the data .

ArrayList keys = new ArrayList("1","2","3");
ArrayList vals= new ArrayList("cat","dog","rat");

So when I iterate over the secomd arrayList - "vals" will i always
get the values in the order - cat , dog , rat ?

If not - then could someone please suggest what I need to do ?

Thanks ,
-anagha
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. A List, by definition, always preserves the order of the elements. This is true not only of ArrayList, but LinkedList, Vector, and any other class that implements the java.util.List interface.
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, this could have been tested in 60 seconds (or less than the time it took to write the post) by just creating an ArrayList yourself, adding elements to it and then printing them out. You could then see if it was ordered.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case you'v java5 you can test this:

output:

a
b
c
0


EDIT:
i posted this before reading any answer
[ May 21, 2005: Message edited by: miguel lisboa ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kashif Riaz:
You know, this could have been tested ...



Well, yes, but you can't necessarily count on the results of this kind of test. You don't know if it's a quirk of the implementation, or if it's guaranteed behavior, until you read the documentation. From the javadocs:


An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

 
pendse anagha
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ranchers - for the help
and yes Kashif - I did implement and check - believe me but just wanted some experts here to verify that my understanding was correct
- and as usual excellent people here are ready to help .

Thanks once again to all who have taken the trouble of helping me out

Regards ,
-anagha
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's the Jrebel link at the bottom all about? That's spam isn't it?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch David Keenan.

The link at the bottom for JRebel is not spam; it's just an advertisement.
 
David Keenan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol. thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic