Comparing Arrays and Lists

This section compares arrays and lists. It explains when to use each, how to declare each, how to create each, how to set the value at an index in each, how to get the value at an index in each, and more.

When to use a List or an Array

Use an array when you want to store several items of the same type and you know how many items will be in the array and the items in the array won’t change in order or number. Use a list when you want to store several items of the same type and you don’t know how many items you will need in the list or when you want to remove items from the list or add items to the list.

Declaring an Array or List

Declare an array using type[] name. Examples are shown below.

int[ ] highScores = null;
String[ ] names = null;

Declare a list using List<Type> name. If you leave off the <Type> it will assume Object.

Remember that lists can only hold objects so if you want to store numbers use Integer rather than int. Integer is a class name and an object of that class can hold an integer value.

List<Integer> highScoreList = null;
List<String> nameList = null;

Note

Remember that declaring an array or list doesn’t actually create the array or list. It creates a reference to an array or list object. You still need to actually create the array or list object.

Creating an Array or List

To create an array use new type[num]. You can do this when you declare the array.

int[ ] highScores = new int[5];
String[ ] names = new String[5];

To create a list use new ArrayList<Type>();. You can do this when you declare the list. If you leave off the <Type> it will assume Object.

List<Integer> highScoreList = new ArrayList<Integer>();
List<String> nameList = new ArrayList<String>();

Note

Note that you don’t have to specify the size of the ArrayList like you do with an array.

Setting the value at an index in an Array or List

To set the value at an index in an array use name[index] = value;.

highScores[0] = 80;

To set the value at an index in a list use name.set(index,value);.

highScoreList.set(0,80);

Getting the value at an index in an Array or List

To get the value at an index in an array use type value = name[index];.

int score = highScores[0];

To set the value at an index in a list use type value = name.get(index).

int score = highScoreList.get(0);

Getting the number of items in an Array or List

To get the number of items in an array use name.length.

System.out.println(highScores.length);

To get the number of items in a list use name.size().

System.out.println(highScoreList.size());

Note

For arrays length is used to get the number of elements and it is a field so it isn’t followed by (). Lists use size(), which is a method call so it requires the ().

Next Section - Common Mistakes