top of page

Arrays

Arrays store a pre-determined and unchangeable amount of data belonging to a specific data type. To manipulate data in an array, we navigate the array using index (indices plural), which are numbered slots in the array. The starting index of any array is always 0.

image.png
image.png

In the code snippet above, two arrays are declared and instantiated as type int. The first array is hard declared with the values 1,2,3 whilst the second array, intArray, is declared but not yet populated.

To populate an array we simply take the array name, use the [] symbol which means that it's an array and put the index number we want to change in it, and set the index to the value we want, much like declaring a variable. 

Notes and restrictions:

  • Arrays can only store items of one data type

  • Arrays are immutable, meaning that the array cannot hold more items than allocated when declaring the array, and the size of the array cannot expand.
    Ex: the second array can only hold 3 values, since only 3 indexes exist in the array

  • Arrays do not have methods, the only "method" arrays can use is .length to find out the length of the given array, as shown below
    (notice that .length doesn't use brackets () like a normal method would, hence why arrays don't really have methods)

image.png
image.png

ArrayLists

ArrayLists are pretty similar to arrays, but have much more use and less restrictions. Arraylists are like arrays but are only able to store objects, can change in size and have methods.
In order to use ArrayLists, you must import them using import java.util.ArrayList;

Screenshot 2024-01-06 203407.png
image.png

In the code segment above is a demonstration on the most common and useful methods for ArrayLists
​.add(obj objName)  // adds value to end of the arraylist

.add(int index, obj objName)  // adds value to specified index

.set(int index, obj objName) // sets index to new value

.remove(index) // removes value at index

.remove(obj objName) // automatically finds first instance of value and removes it from arraylist
.size() // returns the size of the arraylist

.get(int index) //returns the value at the given index

​

Notes:

  • the remove method also returns the value it's removing, so you can also print the value that is being removed in a print statement as shown above

  • When an element is removed from an ArrayList the values to the right of the index being removed will shift one index to the left

  • When an element is added into the ArrayList the values to the right of the index that is being added will shift one index to the right

  • When an element is removed or added to an ArrayList the ArrayList size shrinks and grows respectively. This needs to be taken into account when traversing and working with ArrayLists and even arrays

  • in order to access or traverse the ArrayList we must use the .get(int index) method to have the value at the index returned

2D arrays

2D arrays function the same way as a normal array except with another array layer added. Think of it as Arrays within one larger array

image.png
image.png

In the code snippet above, we instantiate a 2d array, which is of type int. The hard populate a 3 by 3 2d array, meaning the 2d array has 3 arrays in it, with each array having the size of three with the values as show in the snippet.

The 2d array works the same way as a normal array, as in the 2d array's size cannot change as well as the arrays within the 2d array, we use two of the [] symbols to show that it's a 2d array. 

​

There are two major ways to traverse a 2d array, called row major and column major. Both methods use a nested for loop.

​

Row major: 

The outer loop iterates through the size of the total 2d array, (ie the amount of arrays are in the 2d array) or the rows of the 2d array

The inner loop iterates through each value within the inner arrays, or rows.

The first nested loop in the snippet above shows this method, and how it traverses the 2d array by going row through row as shown in the console

​

Column major:

The outer loop iterates through the size of the first array within the 2d array, or columns

The inner loop iterates through the size of the 2d array, or rows

The second nested loop in the snippet above shows this method, and how it traverses the 2d array by going column by column as shown in the console

Notes: 

to access an element in a 2d array, put the specific array you want in the first box and the index of that array in the second box. Ex: if we wanted to access the 9 in the 2d array as shown above, we would say grid[2][2], accessing the third array and the third index of the array

Developping algorythms with arrays

Using arrays we can develop algorithms to find the highest value in the array, lowest value in the array and the average of values throughout the array.

image.png

In the method defined above, we pass in an array of integers with the intention of finding the highest value in the array.
To start, in order to develop any algorithm, common practice is to define a variable of type that is needed to be returned, in this case an int. We set this variable to the first index of the array.

We then use a loop to iterate through each index of the array, comparing each value to highest. If the value at array at i, is greater than the current highest index stored in the variable highest, set highest equal to array at i.
Note: we set the highest variable to the first index of the array, and start the loop that iterates for each index of the array at the second index, since we already have the first index stored inside highest, and don't need to compare highest to itself. 

image.png

In the method defined above, we pass in an array of integers with the intention of finding the lowest value in the array.
We define an int variable called lowest and set it to the value of the first index of the array, this method is the same as the one above except we change the if highest is lower than current index with a if lowest is greater than current index, because by saying this we ask if the current index is smaller than the lowest variable. 

image.png

In the method defined above, we pass in an array of integers with the intention of finding the average of values in the array.
We define an double variable called total, we then iterate through each index of the array and add the value at the current index to the total variable.  
We then return the total divided by the array length to find the average or mean.
Note: We use a double variable instead of an int variable because we need to divide by the total number of numbers. If we had used an int it would have resulted in a float point error and truncate to an int(round down).

image.png
image.png

When we run these methods in main, we can see the highest value is 10, lowest value is 1 and the average of all the numbers in the array is 5.5. 

© 2024 by Samuel Wong.
Powered and secured by Wix

Call

(1) + 437-216-9455

Contact

@Brebeuf STEAM

  • Twitter
  • Instagram
bottom of page