represent collections of related primitive or object reference data using one-dimensional (1D) array objects
If you needed to keep track of 5 exam scores, you could declare separate variables:
int score1;
int score2;
int score3;
int score4;
int score5;
But what if we needed to keep track of 100 exam scores? That would be a lot of variables!
Many programming languages have a simple data structure for a collection of related data that makes this easier.
In many languages, this is called a list but in Java it is called an array.
An array is a block of memory that stores a collection of data items (elements) of the same type under one name.
Arrays are useful whenever you have many elements of the same type that you want to keep track of, but you don't need to name each one.
Instead you use the array name and a number (called an index) for the position of the item in the array.
You can get a value from or store a value to an array using an index.
You can make arrays of ints, doubles, Strings, and even classes that you have written like Students.
In most programming languages, the first element in an array is at index 0.
This is similar to how characters in a String are indexed in Java — the first character is at index 0.
DECLARING AND CREATING AN ARRAY
When we declare a variable, we specify its type and then the variable name.
To make a variable into an array, we put square brackets after the data type.
For example, int[] scores means we have an array called scores that contains int values:
int[] scores;
The declarations do not create the array.
Arrays are objects in Java, so any variable that declares an array holds a reference to an object.
If the array hasn't been created yet and you try to print the value of the variable, it will print null (meaning it doesn't reference any object yet).
You can use the keyword new to get new memory or use an initializer list to set up the values in the array.
USING NEW TO CREATE ARRAYS
To create an empty array after declaring the variable, use the new keyword with the type and size of the array (the number of elements it can hold).
This will actually create the array in memory.
Here is an example of declaring an array variable:
int[] highScores;
Here is an example of creating the array using the new keyword:
highScores = new int[5];
Typically, I will declare and create an array in one step!
int[] highScores = new int[5];
Remember, the number in the square brackets is the size of the array. So, when you create an array with five elements, all five values initialized to default values.
The default values for the different data types are:
0 for elements of type int
0.0 for elements of type double
false for elements of type boolean
null for elements of type String
INITIALIZER LIST TO CREATE ARRAYS
Another way to create an array is to use an initializer list.
You can initialize (set) the values in the array to a list of values in curly brackets {} when you create it!
In this case you don't specify the size of the array, it will be determined by the number of values you include.
Here are a couple of examples of initializer lists:
When you create an array of primitive types (int, double, boolean, etc.) with initial values specified, space is allocated for the specified number of items of that type and the values in the array are set to the specified values.
When you create an array of an object type (like String) with initial values, space is set aside for that number of object references.
The objects are created and the object references set so that the objects can be found.
ARRAY LENGTH
Arrays know their length (how many elements they can store).
It is a public read-only instance variable so you can use the dot-notation to access the instance variable (arrayName.length).
Dot-notation is using variable name followed be a . and then the instance variable (property) name or method name.
Notice that .length returns the number of elements in the array!
Note: length is an instance variable, NOT at method.
So unlike the String .length() method, you don't add parentheses after length.
If you accidentally include the parentheses on the exam, you won't lose points.
ACCESS AND MODIFY ARRAY VALUES
To access the items in an array, we use an indexed array variable which is the array name and the index inside of square brackets [].
Remember that an index is a number that indicates the position of an item in a list, starting at 0.
An indexed variable like arrayname[index] can be used anywhere a regular variable can be used, for example to assign a new value or to get a value from the array:
highScores[0] = 99;
System.out.println(highScores[0]);
Remember, the first value in an array is stored at index 0 and the index of the last value is the length of the array minus one (since the first index is zero).
PARALLEL ARRAYS
If you want to keep track of the top 5 highest scores in a game and the names of the people with those scores, you could use two parallel arrays.
One array could keep track of the scores and the other the names.
You have to make sure to keep them in the same order so that the same index can be used to get the name of the person and their score:
ARRAYINDEXOUTOFBOUNDSEXCEPTION
What happens if you try to access an element that isn't in the list?
For example, what if you try to access the highScore or name at index 5?
The highest index in the two lists is 4...
So, the index must be between 0 and the length of the array - 1 or it will give an error message called ArrayIndexOutOfBoundsException.
DATA ABSTRACTION
One powerful feature in the array data abstraction is that we can use variables for the index as long as the variable holds an integer!
For example:
ARRAYS GAME
You can play the game below to practice arrays.
Click on Arrays and click the element of the * array that would be printed out by the given code.
If you're stuck, check on Labels to see the indices.
You are encouraged to play with a partner to see how high of a score you can get!
Playing this game is optional but should help your understanding of arrays.
SUMMARY
Arrays represent collection of related data all of the same data type.
The size of an array is established at the time of creation and cannot be changed.
Arrays can store either primitive data or object reference data.
When an array is created using the keyword new, all of its elements are initialized with a specific value based on the type of elements:
elements of type int are initialized to 0
elements of type double are initialized to 0.0
elements of type boolean are initialized to false
elements of a reference type are initialized to the reference value null. No objects are automatically created.
Initializer lists can be used to create and initialize arrrays.
Square brackets [ ] are used to access and modify an element in an array using an index. The indexed array variable, for example array[index], can be used anywhere a regular variable can be used, for example to get or assign values.
The valid index values for an array are 0 through one less than the number of elements in the array, inclusive. Using an index value outside of this range will result in an ArrayIndexOutOfBoundsException being thrown.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.