ARR - Array, Create and Access (Lesson)
Array, Create and Access
Introduction
At this point we have learned how to write basic programs in Java. We have learned how to take user input and create output for many different programs. We also know how to traverse programs with loops and skip lines of code when necessary. Imagine now that we want to do all these things but with very large amounts of data. Imagine that you have a class list of students and you want to be able to use the list in a program. There must be a way to traverse and manipulate the list within our program. That is what we will learn to do next. We will be working with large sets of data and learning how to manipulate it without having to copy each individual item.
An array is a group of items that are all the same type. For example, you may have an array of student grades, or an array of names…maybe even an array of car objects.
To declare an array, use the array type, square brackets, reference variable, and the new keyword.
int [ ] grades = new int [5];
The second set of square brackets holds the length of the array. The 5 represents how many items this array will hold.
The square brackets at the beginning can be placed before or after the reference variable:
int grades [ ] = new int [5];
Like Strings, arrays have indices to keep track of each item. The indices start at 0 and go through length – 1.
index | 0 | 1 | 2 | 3 | 4 |
grades | 0 | 0 | 0 | 0 | 0 |
When an int or double array is created, the initial elements are 0 or 0.0, respectively. For a String array, the initial elements are null. A boolean array has initial values of false. These are the default values until you initialize with another value.
The array can be initialized in a couple of different ways. The first way is to initialize each position of the array individually:
0 | 1 | 2 | 3 | 4 |
95 | 90 | 100 | 80 | 86 |
Another option is to declare and initialize the array in one step using an initializer list:
Still another way:
Length of an array
The length of an array is a constant value that is determined when an array is created. To find the length of an array, simply use the name of the array followed by .length.
Notice that this is different than the String length() method. The .length() (with parenthesis) is a method used to find the length of Strings while .length (without parenthesis) is an attribute representing the length of an array.
Click on "Runestone Academy" below to open the required reading that is listed.
Read: Runestone Academy: AP CSA – Java Review Links to an external site.:
- 8.1 – Arrays in Java
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Read: Introduction to Computer Science Using Java Links to an external site.:
- Chapter 60: Arrays
- Complete the quiz for practice.
Arrays and the Magpie
This is an extension to the Magpie Lab.
In this section, we will look at making our code more efficient by utilizing the array structure. Recall that currently default responses are handled using an if statement in getRandomResponse. While this certainly works, as we add more options to the if statement it becomes long and hard to read. An array is an excellent solution when we have a list of options. We can store each option into an array and then select one of the elements.
For example,
Magpie Incorporating Arrays Activity
Add arrays to the Magpie class:
- Create a private instance variable array of type String and fill it with the current responses in getRandomResponse.
- Modify the getRandomResponse method to generate a random number to select an element from the array of responses and return it.
Practice-It! Practice
Go to the PracticeIt website
Links to an external site..
Log into account.
Click on Start Practicing!
Go to the most recent edition.
Click on Chapter 7: Arrays.
Complete Self-Checks: 7.1 – 7.3, & 7.5 – 7.9
IMAGES CREATED BY GAVS