bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Tutorial
Java•Java Tutorial

Java Arrays

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Arrays?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___[] cars;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
Change an Array Element
Access the Elements of an Array

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets [ ] :

String[] cars;

We have now declared a variable that holds an array of strings. To insert values to it, you can place the values in a comma-separated list, inside curly braces { } :

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40};

Access the Elements of an Array

You can access an array element by referring to the index number.

This statement accesses the value of the first element in cars:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo

Note

Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

Think of an array as numbered boxes, where each box stores an element:

IndexElement
0Volvo
1BMW
2Ford
3Mazda

Change an Array Element

To change the value of a specific element, refer to the index number:

cars[0] = "Opel";

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo

Array Length

To find out how many elements an array has, use the length property:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4

The new Keyword

You can also create an array by specifying its size with new . This makes an empty array with space for a fixed number of elements, which you can fill later:

Example

String[] cars = new String[4]; // size is 4
cars[0] = "Volvo";
cars[1] = "BMW";
cars[2] = "Ford";
cars[3] = "Mazda";
System.out.println(cars[0]); // Outputs Volvo

However, if you already know the values, you don't need to write new . Both of these create the same array:

Example

// With new String[] cars = new String[] {"Volvo", "BMW", "Ford", "Mazda"};
// Shortcut (most common) String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Note

You cannot write new String[ 4 ] {"Volvo", "BMW", "Ford", "Mazda"} .

In Java, when using new , you either:

  • Use new String[4] to create an empty array with 4 slots, and then fill them later
  • Or use new String[] {"Volvo", "BMW", "Ford", "Mazda"} (without specifying the number of elements) to create the array and assign values at the same time

Tip

The shortcut syntax is most often used when the values are known at the start. Use new with a size when you want to create an empty array and fill it later.

Previous

Java Break and Continue

Next

Java Arrays Loop