Monday, August 8, 2016

JAVA ArrayList


The ArrayList Collection is similar to the basic array structure containing elements ordered in sequential index location starting with 0 and increasing by +1.  A major difference between the ArrayList and an array is the ArrayList has the ability to change size while the program is executing while an array does not.


The ArrayList is able to change size by simply copying all the data into a new larger or smaller array when needed. This can be slow if there are many elements, but retrieving data is fast using the index locations to directly look up the elements needed.


When to Use Arrays Instead

-When you have very time-sensitive code - ArrayList processing is slightly slower
-When you want to construct Multi-dimensional Arrays - much trickier for ArrayList
-When you want to use the [] notation in the rest of your code to quickly call up an element


As stated previously, the ArrayList List Interface contains special methods in addition to those available from the basic Collection Interface.


Methods Available in the List Interface


- add new elements  (add)
- add an element at a specific index location - add (int index, E element)
- remove elements  (remove)
- remove an element at a specific index location - remove (int index)
- clear all collection (clear)
- search (contains)
- checks to see if the list contains another list - containsAll(list)
- get size of collection (size)
- re-assign the value of a specific index location - set (int index, E element)
- get the current value of a specific index location - get (int index)

- checks to see if the collection is empty (isEmpty)
- search for the first index number of an object - indexOf (Object o) - search for the last index number of an object- lastIndexOf (Object o)

- return an iterator over the elements in the collection (iterator)
- return an iterator that can move forwards or backwards - listIterator()

- returns a string version of the array - toString()
- checks to see if the list is the same as another list - isEqual(list)


The index locations starts with 0 and increases by integers of +1

Adding or removing elements using the list interface shift all other elements down (if adding) or up (if removing).



Initializing an ArrayList

When initializing an ArrayList it is helpful to do 2 things:

1) Use the more basic List callout for the first collection type - this will help to change the type easily if needed later

2) You can give the ArrayList an initial array size - this only has to be an approximate size, and helps to avoid several re-sizing steps when you begin to add elements to the ArrayList for the first time

For example, an ArrayList initialization may look like the following:


List<String> stringArray = new ArrayList <String> (50);

This calls out an approximate initial size of 50 elements for the ArrayList.


Using Methods on an ArrayList

The following example creates a custom method to check to see if a list of names begins with a letter of choice, then returns a new list containing only the names that meet that requirement.  Finally, the list of names is printed.  This example will assume a list of names 50 names long has already been loaded into an array called arrayNames.


// Needed import for the ArrayList generation

import java.util.*; 

// Initialize a custom method that uses a letter input and an array of names input

public static List<String> checkNames(char letter, String [] arrayNames) {

//Creates an ArrayList to store names beginning with the provided letter
List<String> stringArray = new ArrayList <String> (50);

for(String name : arrayNames) {


if(name.charAt(0) == letter) {
stringArray.add(name);
}
}
return stringArray;
}



Calling the method and printing the resulting list of names would look like this:


char letter = 'A';

String [] selectNames = checkNames(letter,arrayNames);



for  (int i=0; i < selectNames.length; i++)   {

out.println( selectNames[i] );
}


No comments:

Post a Comment