Unit 3 Lesson 1 - One Dimensional (1D) Arrays

Lesson summary and assignments

data structure
a structure for organizing, processing, retrieving, and storing data
element
a single value or object in a data structure
one-dimensional (1D) array
a data structure that holds multiple values of the same data type
colors
Orange
Yellow
Blue
Red
Green
0
1
2
3
4
    //Declare and instantiate array
    String[] colors = new String[5];

    //Initialize the array elements
    colors[0] = "Orange";
    colors[1] = "Yellow";
    colors[2] = "Blue";
    colors[3] = "Red";
    colors[4] = "Green";

    //Print the number of elements in the array
    System.out.println(colors.length); //Prints 5
                
  • All elements are of the same type (in this case: String)
  • Index starts at zero
  • Last index is length of the array minus 1

Alternatively, you can initialize the array with an initializer list. The number of entries in the initializer list determines the size of the array. In this case, temperatures.length returns 6

    //Declare a temperatures array and initialize it with 6 values
    double[] temperatures = {78.3, 87.1, 79.8, 92.3, 88.0, 81.6}
                

Vocabulary

data structure
a structure for organizing, processing, retrieving, and storing data
element
a single value or object in a data structure
one-dimensional (1D) array
a data structure that holds multiple values of the same data type