Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

If you want an array that can grow and contain duplicate elements, use


  java.util.ArrayList .

We assume it will rarely contain more than 90 elements.

Don't create, if it already exists.

We add two String references.

Insert "pasito" in between.

Search for "delante".

If found, change to "atras".

We get an Object reference, if we ask for the element at position pos.

We know our array only contains Strings, but, before 1.5, this is the safe way to do it.

This has become easier with Java 1.5 (Java 5) Generics, because it is guaranteed that a String is returned, ...

provided the List has been declared and created as one for holding String type elements only.

We then traverse the array,

forth

and back,......

by using ListIterator .

If we use 1.5 generics,...

these casts to String will become superfluous.

Easy to sort a List. Note that we use the class


  java.util.Collections
ending in 's' , not to be confused with the Collection interface.

Fast searching in an array sorted ascending . Especially important for long arrays.

Sort the array descending.

Remove "pasito".

Empty the array.

==================

If you need to use an array as stack or queue, use
  java.util.LinkedList

instead. It has
  addLast
  removeLast
  addFirst
  removeFirst

methods.

It's much slower, though.

=========

NOTES

=========

You can sort and search a classic array,

, by using this class

ending in 's' .

Sort and FAST search, -1 if not found .

Same, but now with an array of Strings.

=========

If your legacy code is compiled with a 1.5 compliant compiler, it may issue a lot of warnings having to do with generics. Your code will still work, though. The collections are then considered to be 'raw type' collections. A tool like Eclipse can do the conversion for you. Choose Refactor, Infer Generic Type Arguments, Preview.

.