Arrays and ArrayLists in Java

Arrays

Arrays are very useful to know because they are a standard that is built into C, its children languages (Java, C++, etc) and other languages such as Python. An array allows the programmer to store multiple different variables, of the same type, into a single list that can be called upon as a certain object. This, coupled with loops, makes it unnecessary for the programmer to know how many variables are being stored. It does this by allowing the programmer to create one thing (the array) instead of creating multiple different variables that would all be individually initialized and named.

Initializing an Array

To initialize an empty array, the variable type and size must be defined. This is done like so:

type[] name = new type[size];

To use an actual example:

Arrays

Now there is a list that can hold ten integers that is called numlist.

Arrays can also be defined with known information like so:

type[] name = {value,value,value,value,value};

Note: the above can be filled with as many values as needed.

Example:

Screen Shot 2016-03-03 at 12.58.45 PM

Note that Strings require quotes whereas ints would not.

It is important to note that arrays have a set size that does not change.

Accessing an Array

To access the information stored in an array, it must be called using the corresponding position.

Note: the positions of an array, just like a loop, start at 0. Therefore, the fourth piece of information stored, is stored at position 3.

The way to access information from an array is by calling the position, like so:

name[position];

This can be used for all arrays regardless of variable type.

Example:

Screen Shot 2016-03-03 at 12.58.57 PM

This looks at the list of numbers and grabs the int at position 1 (the second number in the list) and stores it in the new variable named “num1”.

Adding to an Array

Once the array is initialized, it is important to know how to add to it. The way to add something to an array is to reference the position where the information is to be added. As mentioned above, the positions of an array start at 0 rather than 1 so the nth piece of information is at position n-1.

For example, we will add the number 12 to the 4th position (making it the 5th piece of information in the array):

Screen Shot 2016-03-03 at 12.59.16 PM

The length of an Array

It is very useful to know the length of an array to be able to access information by comparing it to the end of the list as opposed to the front.

For example, to be a able to access the second last variable in an array, this command would be used:

Screen Shot 2016-03-03 at 12.59.43 PM

Note: since arrays start at 0, they also end at length-1. For example, if there is an array of length 10, it would have positions 0-9. But the length is still 10.

ArrayLists

ArrayLists are an Object Class that work very similarly to an array, but they have a dynamic size. This means that they are essentially an array with no set length (size). But since it is an object class that is built into Java, it can only be used in Java. That is why arrays are still important to know to be able to code in other languages.

Importing the Java Library

Since ArrayLists are not built into the standard library, they need to be imported. They can be imported like so:

Screen Shot 2016-03-02 at 11.38.23 AM

or

Screen Shot 2016-03-01 at 1.04.58 PM

The first one only imports the ArrayList Class. The second imports the entire utility library. This is useful if you are using more than one thing from a single library, like the Scanner and ArrayList Classes.

Instantiating an ArrayList

Creating a new instance of an ArrayList is different than creating a new array. It looks like this:

ArrayList<type> name = new ArrayList<type>();

Now with real code:

Screen Shot 2016-03-03 at 1.14.20 PM

Now there is an ArrayList that can accept variables of type String.

Accessing an ArrayList

Once instantiated, an ArrayList can be accessed in a similar way to arrays, in the sense that it can be accessed by addressing the position that the information is stored in.

Note: Again the position system starts at 0, and any position is its number in the list minus 1.

The main difference between an array and an ArrayList is the way we access the information. Since the ArrayList is an Object, we need to access the data via methods.

The command to access the information is:

listname.get(index)

To use the ArrayList above:

Screen Shot 2016-03-03 at 1.14.34 PM

This will return the String at position 3 (the fourth String in the list) from the ArrayList called “S”.

Adding to and Modifying an ArrayList

Unlike arrays there are two different ways to add to an ArrayList. One is just to add to the end of the ArrayList and, if necessary, it increases the size. This is looks like:

Screen Shot 2016-03-03 at 1.14.46 PM

The other allows the programmer to insert into the middle of an ArrayList, which then shifts everything accordingly. This looks like:

Screen Shot 2016-03-03 at 1.14.55 PM

To be able to overwrite a current piece of information, the set command is used. This looks like:

Screen Shot 2016-03-03 at 1.15.06 PM

This does not shift anything. It simply overwrites the current information.

The other most important modifier is the remove command. The remove command has two forms:

Screen Shot 2016-03-03 at 1.15.14 PM

and

Screen Shot 2016-03-03 at 1.15.22 PM

The first removes the variable at that index. The second removes the first occurrence of that object/variable.

The final modifier allows the programmer to remove everything from the list. This modifier method is the clear method. This looks like:

Screen Shot 2016-03-03 at 1.15.29 PM

The Length (Size) of an ArrayList

The length of an ArrayList is called its size. The size can be used to access the last variable stored in the ArrayList. It can be obtained by calling the size method. The size method returns a numeric value that is the length of the ArrayList. It looks like:

Screen Shot 2016-03-03 at 1.15.37 PM

This is especially useful because the size of an ArrayList is dynamic. It is constantly changing. It is also useful to know if the ArrayList is empty. This can be done by checking if

Screen Shot 2016-03-03 at 1.15.48 PM

but as a way to make it easier on programmers, there is a built in method to check this. It looks like this:

Screen Shot 2016-03-03 at 1.15.57 PM

This returns a boolean value. It can be used in if statements or loops.

Conclusion

Both of these are very useful to know for different situations. ArrayLists are more dynamic and allow for more ease of use as they allow you to have infinitely  sized lists. Arrays are more useful to know because they are more widely used across programming languages. They both have their merits and uses.

Please follow and like us:
Facebook
Facebook
Google+
Google+
http://aiclarke.com/2016/03/03/arrays-and-arraylists-in-java">
YouTube
LinkedIn
RSS