QUICK REFERENCE: CLASS ORDERED COLLECTION

To create an Ordered Collection object we need to write the usual "new". Something like:

            |oC|
            oC := OrderedCollection new.

Like in the previous lesson, we have some mini-exercices to show all the methods working.


add: 'x'

To add the string x after the last element of the collection.

addFirst: 'x'

To add the string x at the first position of the collection.

size

Returns the quantity of elements of the collection.

asString

Convert an element of the collection to String.

at:n

Returns the string in the position n.

Example

| oC |
oC:= OrderedCollection new.
oC add: 'Mary'.
oC addFirst: 'John'. 
oC add: 'Carl'.
1 to: oC size do: [
:i | i+1.  
Transcript show:   ((oC at: i),'\')withCRs . ]

IMPORTANT:Look how we did a loop reading all the elements of the collection. We have used the "formula" we have seeing in previous lesson:

1 to: last do: [
:i | i+1. 
 
"do something" 
 
 ]

We have used also two methods of the object String (an element of the OrderedCollection is an String object) to concatenate a '\' and to do the carriage return (like we have seen in the previous lesson)

Result

John
Mary
Carl

at:n put:'x'

Replace the element at the position n by the string 'x'

Example

| oC  |
oC:= OrderedCollection new.
oC add: 'John'.
oC add: 'Mary'.
oC add: 'Carl'.
oC at: 2 put: 'Peter'.
1 to: oC size do: [
:i | i+1.  
Transcript show:   ((oC at: i),'\')withCRs . ]

Result

John
Peter
Carl

add:'x' afterIndex: n

add:'x' after: 'y'

add:'x' before: 'y'

Example

| oC  |
oC:= OrderedCollection new.
oC add: 'John'.
oC add: 'Mary'.
oC add: 'Carl'.
oC add:'Peter' afterIndex: 1 .
oC add:'Hans' after: 'Mary' .
oC add:'Primo' before: 'John' .
1 to: oC size do: [
:i | i+1.  
Transcript show:   ((oC at: i),'\')withCRs . ].
 

Result

Primo
John
Peter
Mary
Hans
Carl

addAll: oC

Add the new Ordered Collection oC after this Ordered Collection.

addAllFirst: oC

Add the new Ordered Collection oC before this Ordered Collection.

Example

 | oC1 oC2 |
oC1 := OrderedCollection new.
oC1 add: 'John'.
oC1 add: 'Mary'.
oC1 add: 'Carl'.
oC2 := OrderedCollection new.
oC2 add: 'Hans'.
oC2 add: 'Peter'.
oC2 add: 'Andre'.
 
oC1 addAllFirst: oC2.
1 to: oC1 size do: [
:i | i+1.  
Transcript show:   ((oC1 at: i),'\')withCRs . ]. 

Result

Hans
Peter
Andre
John
Mary
Carl

copyFrom: i1 to: i2

Returns this Ordered Collection from the index i1 to i2. A new Ordered Collection.

Example

| oC1 oC2 |
oC1 := OrderedCollection new.
oC1 add: 'John'.
oC1 add: 'Mary'.
oC1 add: 'Carl'.
 
oC2:= oC1 copyFrom: 1 to: 2. 
 
1 to: oC2 size do: [
:i | i+1.  
Transcript show:   ((oC2 at: i),'\')withCRs . ]. 

Result

John
Mary

reversed

Returns this Ordered Collection having the elements in the reverse order.

Example

 | oC1 oC2 |
oC1 := OrderedCollection new.
oC1 add: 'John'.
oC1 add: 'Mary'.
oC1 add: 'Carl'.
 
oC2:= oC1 reversed. 
 
1 to: oC2 size do: [
:i | i+1.  
Transcript show:   ((oC2 at: i),'\')withCRs . ]. 
 

Result

Carl
Mary
John

removeAt: i

Remove the element at the index

Example

| oC  |
oC:= OrderedCollection new.
oC add: 'John'.
oC add: 'Mary'.
oC add: 'Carl'.
 oC removeAt: 2.
1 to: oC size do: [
:i | i+1.  
Transcript show:   ((oC at: i),'\')withCRs . ].
 

Result

John
Carl

removeFirst

removeLast


PREVIOUS LESSON NEXT LESSON
TABLE OF CONTENTS HOMEPAGE