• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java array an ordered list?  RSS feed

 
Paul Nijssen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am studying for the OCA exam and found the following remark in my Sybex OCA-book (page 119): "... an array is an ordered list". What is meant with ordered here? To me it seems that the fact if an array is ordered or not depends on the data in the array.

Best regards,
Paul 
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
Welcome to CodeRanch!

The order refers to the indexes, not the data elements. Sorting refers to the data elements.

So [1,6,4,8] is ordered by not sorted whereas [1,4,6,8] is both ordered and sorted.

In other words, ordered means each time you iterate through the list, you'll get the elements in the same order.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ordered" means that the elements are in a defined order. This is not the same as "sorted", as Jeanne explained.

As an example, think about a bag. A bag holds items, but not in any specific order. You can stick your hand in the bag and pull out items, but the order in which you get the items out of the bag is undefined. In Java, a java.util.HashSet is an example of an unordered collection.

On the other hand, arrays and lists (all classes that implement interface java.util.List) are ordered. The elements are stored in a specific order in an array or java.util.List.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been told off for calling Lists ordered. It would be more precise to say that iterating any collection or data structure produces elements in an order. There are three general categories:-
  • 1: Loosely described as ordered: there is a relation between which element was added first and which element is found first in iteration. Lists, Queues and Stacks usually fall into thie category.
  • 2: Loosely described as sorted. There is a relation between the values of the elements and the order they are found on iteration. Sorted sets and sorted maps fall into this category. Usually the “smallest” element appears first and the “largest” last.
  • 3: Loosely described as unordered. The order in which elements are found on iteration is not readily predictable. Hash sets and hash maps fall into this category.
  • Note that it is sometimes possible to change the iteration order: for example a List can be sorted or shuffled (sort of randomised). Also Lists and Queues are usually iterated in order. Stacks are not usually iterated, but repeatedly popping the top element will yield elements in reverse order of insertion.

    A sorted collection probably stores its elements in order of magnitude, in which case it is not at all loose to call it sorted.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!