Skip to content

Lesson 5 - Arrays & Dictionaries

We've covered the basics of objects, and now we're getting a bit more abstract with two abstract (but very useful) data structures: arrays and dictionaries.

Arrays

Like a set in mathematics, arrays are an ordered list of a particular data type (primitives, objects, other arrays, etc.). They allow you to store and access data in a convenient way, without needed to declare a separate identifier for each variable. Follow along on an IDE as we explore how to use them:

Initialization

There are two ways to declare an array of integers:

// First method
int intArray[];

// Second method
int[] intArray;

Of course, int can be replaced with any other data type. However, when initializing an array, you'll also need to specify the maximum number of elements that it has:

int numElements = 25;
intArray = new int[numElements];
int newIntArray = new int[numElements + 200];

When an array is initialized, all elements are set to 0, false, or null at first, depending on the data type of the array. Alternatively, an array can be explicitly initialized with values:

int[] explicitArray = {25, 225, 2225};

Accessing values

In Java (and most other languages), arrays are zero-indexed, which means that the first element of the array has an index of 0, while the last element of the array has an index of array.length - 1. Individual elements are accessed by putting the index number in brackets next to the array's identifier. For example,

System.out.println(explicitArray[0]);
System.out.println(explicitArray[explicitArray.length - 1]);
System.out.println(explicitArray[2]);

would output

25
2225
2225

Using a for loop, you can access values by iterating through the indices of the array. For any array, a general model to input values would be:

Scanner sc = new Scanner(System.in);
int size = sc.nextInt();

int[] anArray = new int[size];

for(int i = 0; i < anArray.length; i++){
    anArray[i] = sc.nextInt();
}

The same applies for objects; an Person array (see previous lesson) can be created as follows:

Person[] personArray = new Person[size];
for(int i = 0; i < personArray.length; i++){
    String name = sc.next();
    personArray[i] = new Person(name);
}

Check understanding

How would you print the contents of an integer array? In ascending order? In descending order?

View solution
// Ascending order
for(int i = 0; i < intArray.length; i++){
    System.out.println(intArray[i]);
}

//Descending order [solution 1]
for(int i = intArray.length - 1; i >= 0; i--){
    System.out.println(intArray[i])
}

//Descending order [solution 2]
for(int i = 0; i < intArray.length; i++){
    System.out.println(intArray[intArray.length - i - 1]);
}

Exercises

  • Given an integer array, compute the product of the first and last elements of the array
  • Given an integer array, compute the average value of the in the array
  • Given a string array, print the concatenation of the strings with even indices (hint: how would you modify the for loop to iterate through the desired values?)
  • Find the maximum value in an integer array
  • Modify an integer array such that all positive values are replaced with 1 and all negative values are replaced with -1

ArrayLists

While arrays in Java have a fixed size when they are initialized, ArrayLists are objects that allow for dynamically-sized arrays. This is useful when you do not know how many objects are in an array, such as match entries in the scouting app.

Initialization

The syntax for initializing or declaring a new ArrayList is very similar:

//Just declaration
ArrayList<Integer> arrayList;

//Initializing an empty ArrayList
ArrayList<Integer> emptyList = new ArrayList<>();

//Initilizing an ArrayList with 25 elements
ArrayList<Integer> thirdList = new ArrayList<>(25);

References

Enhanced for loops

2D Arrays

Enumerations

HashMaps

HW: A Bank Network