Learn how to store data in increasingly complex ways!
Storing a piece of information in Java is a relatively simple process. For example, if we wanted to store the name of our favorite basketball player, we could do so by storing the name in a variable:
But what if you were asked to store the name of your favorite player on each team? It’s possible to store each of those values in a separate variable, but that process would be hard to keep track of! What would you name all of the variables? favplayer25
? There needs to be a better way to store all of this information so that it can be easily accessed and organized. Luckily, arrays are one such way that we can store data more effectively.
An array is a list of data. Arrays can store many different types of information. In our current example, we can create a list of all of our favorite players on each team:
To create an array in Java, you must first know what type of data you want to store and how many elements the array will hold. In the player list example above, we created an array of Strings
, but it’s just as easy to create arrays of ints
or any other data type. Arrays can store both primitive and object data types.
Arrays can be created in a few ways:
The easiest way of putting data into an array is to initialize the array with data when declaring it. To do so, simply include the array elements between curly braces. Here’s a list of 6 numbers:
It’s also possible to declare a new empty array and add in the list items later. The format for creating an array is:
Let’s take apart the array declaration:
type[]
: This tells us what type of data is being stored in the array. For example, a list of integers would be int[]
. The brackets []
indicate that this is an array and not a single int
.
arrayName
: The name by which the array will be known. It’s best to pick a descriptive name, like groceryList
instead of just things
.
new
: The new
keyword indicates that a new array is being created.
type[numberOfElements]
: The type of elements is mentioned again, as well as the number of items the array will store. For example, if an array int[6]
were created, the array would store six int
values. Once an array has been created, the number of items that can be stored in the array cannot change. This array will always store 6 items - no more, no less!
Elements in an array can be accessed using their index position in the array. Let’s create a new empty array of numbers, then add in two numbers:
The array now contains two integers:
You’ll notice that even though the array was created without assigning any values, the rest of the indices currently store the value 0. When creating arrays with default values, Java assigns a specific value for each item in the array depending on the type. Integers
and doubles
default to zero, booleans
default to false
, and objects (including Strings
) default to null
.
Type | Default Value |
---|---|
int | 0 |
double | 0.0 |
boolean | false |
Objects | null |
[]
syntax can also be used to change elements in a list. Let’s change 13 to 21:
Here is an example program for you to examine. Try changing the value of the items in the array - how does that affect the order of the items in the list? Try choosing a value greater than 4 to put the String
value - what happens when you do that?
Accessing elements in a list uses a similar syntax. We can get and store the number at index 0 into a variable:
The output from the code will be:
Now that you’ve learned about how to create and access arrays, let’s try making one! In the following code editor, create a list of your five favorite TV shows. Then, print out your favorite of those five by accessing the element in the array and printing out the value: