This page will define arrays and lists, how they’re used, and other details.
- What is an Array?
- What is an index of an Array?
- What is a List?
What is an Array?
An array is a data structure that holds a number of related data objects. An array is typically defined with a size in mind, which accounts the number of related data objects it’s allowed to store.
All of the objects in an array must be of the same type, however, you can define the types of objects an array can hold to be different.
As stated, each cell of an array can hold one data object. Furthermore, each cell has its own index, to distinguish it from the rest of the cells. In most usable languages, the index for an array starts at 0.
The syntax for an array is as follows in the C Language:
type <variable_name>[size];
The syntax for an array is as follows in the Java Language:
type arrayName[] = new type[size];
We’re going to be using Java in our examples below.
Here is an example of an integer array of size 3:
int array[] = new int[3];
array = {9,8,7};
At the above example, we are actually setting the values of the array. If we skipped line 2, then all the elements of the array would be undefined.
Let’s take a look at this array, using a picture that I definitely didn’t just create in paint:

What is an Index of an Array?
The index of an array, is the part of the array (as in the example above) inside of the brackets; [index].
So in the above picture, the array[0], the 0 is the index. All of the indexes together are called indices.
You should note that the index starts at 0, not 1. This is an important note in a lot of array-based data structures.
For example, what is the index of the data object 8 in the above example?
Try to answer yourself before looking below:
The value of 8 can be accessed by doing the following:
int numberEight = array[1];
It should be obvious now, but the index of the data object of 8, is 1. So if you have a variable that needs to be set to the value of a specific array, then you access that arrays specific data object’s value by specifying the index.
What if you want to know all the values of an array?
That’s a good question, I’m glad I asked. Do you remember loops, we can loop through an array as follows.
Also, keep in mind, this is something that I’ve used very frequently throughout my programming career.
int array[] = new int[3];
array = {5,6,7};
for(int counter = 0; counter < array.length; counter++){
System.out.println(array[counter]);
}
This prints out the following to the console:
5
6
7
So what are we doing here?
Basically, we are creating a loop with a counter, and that counter is acting as the index to the array. The counter starts at 0, which is the first index of all arrays, and then keeps incrementing until we reach 1 minus the length of the array.
Why is it that we don’t go to the full length? The answer is that since arrays are 0-index based, that the value of the length includes 0.
I know, it sounds confusing, but try to visual this over and over again:
- An array of size 5, has the following indices: 0,1,2,3,and 4
- An array of size 1, has the following indices: 0
- An array of size 3, has the following indices: 0,1 and 2
An array of [size] has [size] number of cells with indices of 0 to [size-1]
I think you might get the picture, if you don’t, uh, I’m sorry I can’t explain it better, but maybe these examples might help:
// Searching for a value in an array
int array[] = new int[5];
array = {5,5,5,6,5};
// find the 6 in the array and print out its index
for(int counter = 0; counter < array.length; count++){
if(array[counter] == 6){
System.out.println(counter);
}
}
So this will print out 3 once the array[appropriate index] reaches the value of 6, then the index is printed.
However, in programming, it’s usually the other way around. You are trying to find a value based on the index, instead of finding the index based on the value.
Let’s do a more complex example:
Let’s reverse the values in an array of size 10:
int tempValue = 0;
int array[] = new int[10];
array = {9,8,7,6,5,4,3,2,1,0};
int arrayLength = array.length;
for(int counter = 0; counter < arrayLength / 2; counter++){
tempValue = array[counter];
array[counter] = array[(arrayLength-1) - counter];
array[(arrayLength-1) - counter] = tempValue;
}
Woah, there’s a lot of stuff happening here, let’s try to go through this step by step:
- We create a temporary value to help us with swapping values from the front to the back of the array (and vice versa)
- We declare an integer array of size 10 and initialize it’s values
- We create an arrayLength value from the array we just created
- We have a loop that goes through half of the array
- If we had it go through the whole list, it would end up reversing it twice, thus getting the same list we start with
- Inside of the loop:
- We set the temp value to the array’s index value from the counter
- We set the array’s index value to the array’s length – 1 and minus the counter
- We set the array’s index value of arraylength -1 and minus the counter to the temp value
I will include more in the Programming Examples section of this site, but until then, just try to grasp the concept of arrays for now.
What is a List?
A list is much like an array, but you don’t have to specify the size in most cases. However, you still have to specify the type.
Lists are mostly a java thing, if you use a list in a C program you’re going to have to deal with pointers, head and tails, etc.
For the simplisity of this page, we are going to focus on the java List structure.
Here is an example of a Java List (ArrayList):
List<int> list = new ArrayList<>();
list.add(2);
list.add(5);
list.add(10);
Ssytem.out.println("What is the size of this list? "+list.size());
System.out.println("What is the value at index 2 of this list? "+list.get(2));
System.out.println("What is the value at index 0 of this list? "+list.get(0));
System.out.println("What is the value at index 4 of this list? "+list.get(5));
System.out.println("Does this list contain the value 5? "+list.contains(5));
System.out.println("Is this list empty?"+list.isEmpty());
list.clear();
System.out.println("Is this list empty?"+list.isEmpty());
This outputs the following to the console:
What is the size of this list? 3
What is the value at index 2 of this list? 10
What is the value at index 0 of this list? 2
What is the value at index 4 of this list? undefined
Does this list contain the value 5? true
Is this list empty? false
Is this list empty? true
Ok so let’s walk through the above example:
- We define a list of ints
- We add 2,5, and 10 to the list, each one being added to the end
- We get the size of the list
- We get the value of the list at index 2, which is 10
- We get the value of the list at index 0, which is 2
- We get the value of the list at index 4, which doesn’t exist, therefore undefined is returned
- We check and see if the list contains a value of 5 inside of any of it’s cells, which it does (at index 1), therefore true is returned
- We check and see if the list is empty (the size of the array is 0), it isn’t (it has a size of 3), therefore false is returned
- We clear all of the elements of the list
- We check again and see if the list is empty (the size of the array is 0, it is (it has a size of 0 since we just cleared it, therefore true is returned
Lists are very handy because you don’t have to keep track of how many data objects you plan to put inside like you do with an array.
Here is a helpful Javadoc link that shows all the methods and features a List can do: