JUSTIN LEONG

Arrays

Arrays are one of the most fundamental concepts in programming and are usually the first data structure introduced to programmers. An array is simply a continuous list of storage containers that holds data. In addition to this, the containers in an array are referred to by their index position starting from 0 on the left most container and increasing by 1 for each container to the right. The advantage of using an array is that the data contained inside of it can be accessed relatively quickly by its index value.
Grasping the abstract concept of arrays can be difficult at first since they are represented as virtual containers in your computer. However, I found it helpful to compare arrays to a physical box used to contain books. Using this analogy, suppose you had 10 books that you wanted to organize neatly so that you could quickly find a book if you ever needed to. Immediately, you would think to find a box that could perfectly fit all 10 of the books. This is similar to creating an array to hold a certain amount of data. The array itself would represent the book container, while the data inside of the array would represent the actual books.
We can take this analogy one step further by describing how the size of an array is defined. If we think about our book container, we want the container to have 10 slots to hold all 10 books. Similarly, we define the size of the array by specifying how many items we want the array to contain. Now if we purchased an 11th book we would not be able to fit it in our container as it would break the box. An array works the exact same way where if we have more data than storage slots we would not be able to added it to our array. To fit the 11th book into our container, we must find a larger container that can fit 11 or more books then move all the books from the old container to our larger one. And as you have probably guessed this is the same for an array, as a new array of larger size must be initialized then each of the items in the old array must be copied to the new array prior to adding the new item.
One important question to ask is why wouldn't we just define an array that can hold a very large amount of data so we don't have to worry about running out of storage containers? The simple answer is that the larger the array size the more memory it takes up in the computer which could be allocated for other purposes. This same question could be asked about our book container, why not find a container that can fit 100 books? or 1000 books? The answer is that if we used a container that could hold up to 1000 books to store only 10 books then the rest of the container would just be taking up space in our room which could be used to place other items such as a bed or a desk. We want to be as efficient with our physical space as possible and this goes for the space/memory we use in the programs we write.

Creating Arrays in Java

With a basic understanding of arrays, we can now move to the actual implementation of creating an array using Java. There are two different techniques used to initialize an array in Java. The difference between the techniques being whether we know the data ahead of time or not.
Suppose we want to create an array of integers that will contain the height of 10 students. We don't know the heights of the students ahead of time, however we do know how many students we have. Therefore in our first technique we will create an array with a size of 10.

int

[] heights =

new int

[10];
In the above declaration, we create an integer array called 'heights' with a size of 10. We know that this is an integer array because of the square brackets following the variable type on the left hand side of the equal operator. On the right hand side of the equal operator we create a new object and specify the size of the array in the square brackets.
If we were to print out all of the integers in the heights array that we created, we would get the following output.

[0,0,0,0,0,0,0,0,0,0]

The reason why the array is filled with 0's is because that is the default value for integer arrays in Java. Java has different default values for arrays of different primitive types and objects. I have listed a few of the common default values below.

int[] :       

[0, 0, 0, 0, 0, 0, 0, 0.....]



double[] :  

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0.....]



boolean[] :

[false, false, false, false, false, false, false, false.....]



Object[] :  

[null, null, null, null, null, null, null, null.....]

Once we obtain the heights of all 10 students, we can go back and update each index position of the array with the correct height value.
The second technique to creating an array is when we know exactly what data should be inside of the array. Suppose we know all 10 heights of our students, we can directly add these heights(represented in cm) to our array as we initialize it.

int

[] heights = {181, 165, 193, 172, 179, 150, 162, 204, 199, 177};
Instead of creating the array then updating the value at each position, we have shown that we can complete this task in one line of code. Being able to create an array is important, however the real power of arrays comes from being able to update and manipulate the values inside. This brings us to the next topic of accessing data in an array.

Accessing Array Data

Being able to quickly retrieve data from an array is one of the huge advantages of this data structure. Accessing data from an array is fairly straight forward, all we have to do is type the name of the array followed by square brackets and entering the index position of the value we want to access inside of the square brackets. This brings us to the next important topic of arrays which is indexing.
An index can be thought of as the storage containers address in an array. Each container has a unique integer value that denotes the position of the value in the array. We already know that arrays are a continuous list of containers that are placed side by side. The index of an array starts off at index 0 on the left most container and increases by one for each container on the right. This idea of having the first item start at an index of 0 can be referred to as zero-based indexing.
If we want to access the first value in the above array we would reference that container by its index value of zero. The following code would print the value '181' to the console.

System.out.println(heights[0]);

//This will print the value 181

The most important thing to remember about arrays is that the indexes are zero-based, therefore if we want to access the last item in the array it will always be the number of items in the array subtracted by 1. It is very common to write code that will cause an IndexOutOfBounds runtime error which in many times can be traced to trying to access the index equal to the number of items in the array which is undefined.

Additional Array Concepts

So far we have only skimmed the surface of arrays, but have built a strong understanding of the fundamentals of this data structure. There are many advanced topics of arrays that you should be familar with in order to become a master of this data structure. Below is a list of more advanced topics that I hope I will be able to create content for in the future.

●   Inserting values at the end, middle, and beginning of an array



●   Deleting values at the end, middle, and beginning of an array



●   Sorting an array (Check out my topics on sorting algorithms)



●   Searching for a value in an array



●   2 Dimensional arrays



●   ArrayLists and dynamically resizing arrays



●   Two pointer technique with arrays