• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

OCA, Chapter 3, Question 18 - Meaning of ordered

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer in page 341 is wrong, it stated that "Both an array and ArrayList are ordered". However, from Oracle's Java Docs  https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html, an array is in order (ordered) when is sorted, and from this is concluded that an Array in not ordered if not sorted.

Can you reference a source where it it's stated that Arrays and Arraylists are ordered?
 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post the answer from the book? I do not understand what you mean.

From what you provided, I believe you are talking about how ordering of elements before they are sorted. Arrays and and ArrayLists only represent a sequence of elements. The elements have no ordering unless you order them yourself. And you do this by sorting. Again, you have to post the answer for me to better understand what you mean.
 
Marshal
Posts: 80874
506
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William Ng is right; we need more details before we can understand why you think there is a problem.
What “ordered” means is that iterating the data structure causes its elements to be visited in a predictable order. You can create an int[] or a List (probably actually an ArrayList) from an IntStream object like this:-Arrays and Lists (say List not ArrayList) are ordered; the order of visiting their elements is predictable and constant. If you don't change their contents or add more elements, both these loops will cause “1 2 69 4“ to be printed:-Note: beware of using the get() method on a linked list; it can cause slow performance.

William Ng wrote:. . . . The elements have no ordering unless you order them yourself. And you do this by sorting. . . .

That is incorrect, I am afraid. Lists and arrays preserve insertion order or creation order. It is possible to alter their order by adding elements or sorting them; it is possible to alter the size of a List object but it is not possible to alter the length of an array object. People talk about ordered collections, sorted collections and unordered collections. In the case of a ordered collection, iteration produces the elements in the order they were added, allowing for the fact that such order can be altered, for example with numbers.add(1, −999_999); It is possible to create ordered collections which iterate their contents backwards. A sorted data structure can be iterated in order of the value of its elements; if you sort numbers in my example, that is changing their order. Some data structures, e.g. this, are sorted automatically as elements are added. Some data structures are called unordered, which is an inaccurate term. If you iterate a HashSet, you cannot easily predict what the order of elements will be, but you will usually get the same order.Remembering that 0x41 is 65₁₀, see what difference you get if you omit 0x41 in line 99, even though you have a set with the same contents; the two versions would return true from their equals() method. Also notice what happens if you run the loop repeatedly: you get the same output.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

I am puzzled by this question, just as the original poster.

The question is:

18. Which of the following are true? (Choose all that apply)
A. ....
B. ....
C. ....
D. An array is ordered.
E. An ArrayList is ordered.
F. ....
G. ....

And both D and E are marked correct, with the following explanation:

[...] Both an array and ArrayList are ordered and have indexes. [...]

 
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Niculae wrote:Hello.

I am puzzled by this question, just as the original poster.

The question is:

18. Which of the following are true? (Choose all that apply)
A. ....
B. ....
C. ....
D. An array is ordered.
E. An ArrayList is ordered.
F. ....
G. ....

And both D and E are marked correct, with the following explanation:

[...] Both an array and ArrayList are ordered and have indexes. [...]



from stack overflow:


An ordered collection means that the elements of the collection have a specific order. The order is independent of the value. A List is an example.

A sorted collection means that not only does the collection have order, but the order depends on the value of the element. A SortedSet is an example.

In contrast, a collection without any order can maintain the elements in any order. A Set is an example.



Thus, because Arrays and Arraylists both have indexes, they are in a specific order. They are not ordered because their positioning is not based on a property of the elements
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zach Rode wrote:. . . Arrays and Arraylists . . . are not ordered because their positioning is not based on a property of the elements

Did you mean to say not sorted?
 
Zach Rode
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Zach Rode wrote:. . . Arrays and Arraylists . . . are not ordered because their positioning is not based on a property of the elements

Did you mean to say not sorted?




Woops. Yes I did. Sorry about that.
reply
    Bookmark Topic Watch Topic
  • New Topic