1. To create and fill an array of any primitive data type.
2. To use a or loop to iterate through an array and print its values. (Using the .length variable)
3. To use a for each loop instead of a regular for loop to iterate through an array.
4. Sorting an array of integers.
An array is a container object that holds a fixed number of values from a single type. In previous lessons, we had to create a single variable for each piece of data we wanted to store. An array is a single object that can store multiple values.
Up until now, if I wanted to store 5 different inputs from a user, I would need to create 5 separate variables. Now we have a single array of length 5, where we can refer to each data type by its index. An index is the location of an element stored in an array.
The index is the location of the element. It starts at 0. So if the length is 10, the index ranges from zero to nine.
To create an array, we must do several things.
1./2. declare the type and put [].
3. give the array a name to refer to it by.
4. assignment operator
5. new keyword
6./7. same data type & length of array in []
This creates an empty array, where we then have to fill the array by its index.
8. name_of_array[index] = value;
and you’ll have to do this for each index.
This way is called an Array Literal. Where we create and fill the array at the same time.
Another way to create an array and fill it at the same time would be:
1./2. declare the type and put [] brackets.
3. give the array a name to refer to it by.
4. assignment operator
5./6. {} braces & the values you want to store inside the braces, separated by commas.
In this example, the creation and filling is done at the same exact time! and the length will depend on how many values you give.
In order to print out an array, we have to call the array by name and in [] refer to the index we want to print. All inside of a system.out.println();
But what if we want to print out everything in the array at the same time. If we had 1000 indexes, printing each index individually would really.... stink.
In order to print out an entire array, we have to use a for loop (or while loop) to iterate through each index and print it out. The .length variable of an array becomes very handy here.
If we use the array_name.length; It'll return the length of the array for our use!
Yes!
In order to change the value at a specific index, we just refer to that index and assign it a new value.
Short Answer: No
Once we give an array the length of 5, that cannot be changed, so we cannot add a 6th element.
If you try to refer to an index that is greater than or equal to the length, you'll receive an ArrayIndexOutOfBounds Exception.
Long Answer: With some trickery.
If we create a new array with a bigger length, fill it with all the old values, we technically increased the size.
Filling an array with a for loop is going to look very similar to what we already have seen. In the gist to the right, we are iterating through each index of the array and giving it a value. This value also uses the for loop variable.
The gist to the right assumes you have already imported a scanner.
Inside the for loop, we can ask the user for input and store it in the array. This works because we use the for loop variable as the index for the array which changes every loop so we are not saving over the same exact index.
There is another type of for loop, so why haven't I shown it yet? This loop does not work without a type of collection or list. Simply, we hadn't worked with the structures necessary for it to be used. Now that we know arrays, this loop is a better option for iterating through them.
In order to do a For Each loop:
Pro Tip: The for loop does not have a variable to access each index and that makes it very hard to use this loop when wanting to get input and resave it. Just because it looks easier, doesn't mean it's good for all use cases.
Arrays can be used as arguments all the same, with one stipulation. When we pass an array, we are passing the access to it. Therefore, any changes we make to the array are changed everywhere. It is not a copy like with any other variable.
You should ask the user to input the numbers for an integer array and then you print out which number was the biggest!
W/O using array shortcut methods!
You should ask the user to input the numbers for a double array and then you print out the average number!
W/O using array shortcut methods!
Think the class is too easy for you? Well... Try this.
Ask the user for an integer array, and then sort the array from least to greatest. Do this without the sorting method. Manually sort it yourself! :]