Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OCA, Chapter 3, Question 18 - Meaning of ordered  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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: 48
1
  • Mark post as helpful
  • send pies
  • 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: 58830
179
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!