Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCA, Chapter 3, Question 19 - ordered array/arrayList

 
Petra Miller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the book "OCA - Java SE 8 Porgrammer I - Study Guide" is in chapter 3, the following Questions 18:

Which of the following are true?
A. An array has a fixed size
B. An ArrayList has a fixed size.
C. An array allows multiple dimensions
D. An array is ordered.
E. An ArrayList ist ordered.
F. An array is immutable.
G. An arrayList is immutable.

In the book the correct answers are A, C, D, E

My question is: Why are an array and an arrayList ordered?
Does order mean, that it is sorted? (english is not my mother language)
If I want to order an array I would use Arrays.sort(myArray), for an arrayList I would use Collections.sort(myArrayList). So an array and an ArrayList can not be ordered an answer D and E ist false.

Thank you for your help!
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means given an array or ArrayList, the items have an established order. The first element is the first element, the third element is the third element, etc. Contrast this with a Set, where there is no order. For a regular Set a of elements, there is no first, second or third element, it's just a group of elements.

Sorting is an entirely different issue. Arrays can be sorted by their natural ordering and/or using a comparator. They sorting can also be automatically maintained, such as in a TreeSet. The difference between a sorted and ordered data structure is also what happens when a new element is added. A sorted structure will automatically sort the new element, while an ordered will just add it to the end (or beginning) of the structure.
 
Petra Miller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Petra Miller,

First of all, a warm welcome to CodeRanch!

Petra Miller wrote:My question is: Why are an array and an arrayList ordered?
Does order mean, that it is sorted? (english is not my mother language)
If I want to order an array I would use Arrays.sort(myArray), for an arrayList I would use Collections.sort(myArrayList). So an array and an ArrayList can not be ordered an answer D and E ist false.

There is a difference between ordered and sorted. When a collection is ordered, it means you can iterate through the collection in a specific (not random) order. A sprted collection means that the order in the collection is determined according to some rule(s), known as the sort order. So a collection can be ordered, but unsorted. A sorted collection will always be ordered as well. And a collection can be unordered and unsorted. A collection can never be sorted but unordered.
An ArrayList keeps the order established by the index position of the elements (just like an array). Therefore an ArrayList and an array are ordered (but unsorted) collections (and that's why answers D and E are correct).

Hope it helps!
Kind regards,
Roel
 
Felipe Kunzler
Ranch Hand
Posts: 35
3
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Petra,

I got confused with this same question as well.

In Portuguese we don't have a specific word for "sorted".
Instead, we use "ordered" (ordenado) interchangeably for both "ordered" and "sorted".

E.G. We don't say "Sorting algorithm", we say "Ordering algorithm".
Guess we have a slightly disadvantage here haha!

All the best!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:So a collection can be ordered, but unsorted. A sorted collection will always be ordered as well. And a collection can be unordered and unsorted. A collection can never be sorted but unordered.

Let's provide a code snippet which illustrates all different possibilities. In this code snippet you'll find
1/ an array and ArrayList -> ordered (on position) but unsorted
2/ a TreeSet -> sorted (and thus ordered)
3/ a HashSet -> unordered and unsortedOutput:
array: [Roel, Petra, Java rocks!, CodeRanch]
ArrayList: [Roel, Petra, Java rocks!, CodeRanch]
HashSet: [Roel, CodeRanch, Java rocks!, Petra]
TreeSet: [CodeRanch, Java rocks!, Petra, Roel]


Hope it helps!
Kind regards,
Roel

PS. HashSet and TreeSet are used for demonstration purposes only, these classes are not on the OCA exam.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Felipe Kunzler wrote:In Portuguese we don't have a specific word for "sorted".
Instead, we use "ordered" (ordenado) interchangeably for both "ordered" and "sorted".

We have different words for "ordered" and "sorted", so maybe you should start learning Dutch first
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic