Arrays

An array is a data structure that is similar to the structure of a numbered drawer. There is a limited amount of items the drawer can hold. Once you set the size of an array you cannot change it. To retrieve an item from a drawer you must open the compartment where it is located, similarly, in an array you must access the index number that the item was assigned to in order to get the item. In a drawer all the compartments or boxes have a similar shape; also in an array all the indexes must hold the same type of data (ie array of strings, array of integers…)

 


To create an array we must declare the size and data type and variable name like this: String [] myArray = new String[4];

( DataType [] VariableName = new DataType[SIzeOfArray] ) This will create an empty array that can take 4 stringsarray1

Now suppose this is an array of employee names and we want to put add Sam’s name to the first index of the array. This is how we do that:

myArray[0] = "Sam";

This will put the String Sam into the 0 index. array2

note that array indexes start at 0 not 1.

Now let’s put Lee in index 2, Terry in index 1, and Jose in the last index;

myArray[2] = "Lee";
myArray[1] = "Terry";
myArray[3] = "Jose";

array3
Now we can get the item at any index by simply calling that index number, ie

System.out.print(myArray[2]);
 will print Lee.
myArray[1].equals("Terry");
 will give you true.
myArray[3].equals("Sam");
 will give you false.
Match Array[x] with its proper value or Boolean result

The following is the data in an integer array named myNumbers [2|8|0|9|6|0|6]
Determine the result for each statement.

myNumbers[4]==9;
System.out.print(myNumber[2]);
myNumbers[3]<5;
myNumbers[0]+myNumbers[5]==2;
System.out.print(myNumbers[6]+myNumbers[1]);

Program: Create an Integer array of 3 elements called myIntArray and assign a number to each index

 

Iterating

When we are looking for something in a drawer but we don’t know whether it is in the drawer, we usually look in each compartment from the first compartment to the last. We iterate through the drawer. We previously learned of a tool that we can use to accomplish this task quickly. We use loops to iterate through arrays.

Let’s say instead of having an array of 4 employee names we had an array of over 100 employee names. It will take forever to type a print statement for each index location like this:

System.out.print(myArray[0]);
System.out.print(myArray[1]);
System.out.print(myArray[2]);


System.out.print(myArray[99]);

Instead we can use a for loop to print each name like this:

for(int indexNum=0; indexNum<100; indexNum++){
    System.out.println(myArray[indexNum]);
}

Also suppose we are looking for a specific employee name but we do not know which index number the name is stored in, we can use a for loop to iterate through the array and search for the employee, once the employee is found we will stop the loop to stop the search.

i.e The following code will search for Daniel in an array of 1000 employee names.

for(int indexNum=0; indexNum<1000; indexNum++){
    if(myArray[indexNum].equals("Daniel")){
        System.out.println("Daniel was found at index #"+indexNum);
        break;
    }
}

The break statement stops a loop, when the index number of Daniel is found we don’t need to keep searching so we use break to stop this for loop.

Program: Write a program that iterates through an array of decimal numbers (type double) and only prints the numbers that are less than 3.
Ex an array of the numbers [ 1.2 | 54.5 | 6.3 | 2.56 | 1.74 | 7.23 ] should print 1.2 2.56 1.74

 

Sorting Arrays

Arranging the items in an array from lowest number to the highest number is quite simple. To sort an array you must use the following code:

Arrays.sort( yourArray );

ie the following program creates an array of unordered numbers then puts the number in numerical order.

This program will output:

9
34
72
85
167

Program: Modify your previous program to sort the array before you iterate through it.

 

Multidimensional Arrays

A multidimensional array is like a drawer where each draw has more than one compartment. Each draw can be divided into several mini compartments. If we have an array of employees and we want to hold more information than just the employee name we can use a multidimensional array, the row will represent the different employees and the columns will hold each employee’s information such as name, age, title, salary etc. arrayB

To create a 2-dimensional array we have to use two square brackets instead of one
Ex.

String[][] carArray = new String[4][3];

This will create an array with 4 columns and 3 rows.

Let us say we have an array of 4 cars where row0 is the car’s make, row1 is the car’s model and row2 is the manufactured year.
array4
To retrieve the last car’s model we run carArray[3][1]; this will give us i8.
Program: Write a program that will print out only the info for the Bugatti. The Make Model & Year

An Array is a very commonly used structure for storing data. You have probably realized by now that a string is an array of characters.

Back to Course Menu
What do you think about this topic? Share your suggestions / comments