Understanding Java Arrays and ArrayLists

Textual Information

In Java, arrays are fixed-size data structures that hold a collection of elements. ArrayLists, on the other hand, are resizable and can grow as elements are added.

Click this box to learn more.

Video Information

Code Snippets

See how Arrays and ArrayLists are implemented

int[] numbers = {1, 2, 3, 4, 5};  // Array with 5 elements

// Accessing elements in the array
System.out.println(numbers[0]);  // Outputs 1 (first element)
System.out.println(numbers[numbers.length - 1]);  // Outputs 5 (last element)

// Modifying an element in the array
numbers[1] = 10;  // Changes the second element to 10

// Printing all elements using a for loop
for (int i = 0; i < numbers.length; i++) {
    System.out.print(numbers[i] + " ");
}
// Output: 1 10 3 4 5

// Printing all elements using a for-each loop
for (int num : numbers) {
    System.out.print(num + " ");
}
// Output: 1 10 3 4 5

// 5. ArrayList with Wrapper Classes
ArrayList grades = new ArrayList<>();  // ArrayList of Double objects

// Adding elements
grades.add(85.5);
grades.add(90.2);
grades.add(76.4);

// Calculating the average grade using a for-each loop
double sum = 0;
for (double grade : grades) {
    sum += grade;
}
double average = sum / grades.size();
System.out.println("Average grade: " + average);  // Outputs the average grade

                            

Quiz

Test your knowledge on Java arrays and ArrayLists:

1. What is the value in

array2[array2.length - 1]

After the following code is executed?

double[] array1 = new double[50];
double[] array2 = new double[50];
for(int i = 0; i < array1.length; i++) {
    array1[i] = i + 1;
    array2[i] = array2.length - i;
}
array2 = array1;
for(int i = 1; i < array1.length; i++)
    array1[i] = array1[i - 1] + array1[i];
                                

2. What error will be caused by the following code, and why?

String[] array = new String[100];
System.out.println(array[99].charAt(0) + " is the first 
letter of the last String.");
                                

3. How do you declare an array in Java?

More Information

Arrays

Characteristics: Homogeneous, Static Size, Random Access, Reference Types Only.

Array Initialization

Compile-time: char[] vowels = {'a', 'e', 'i', 'o', 'u'};
General declaration: Type_name[] variable_name = new Type_name[i];
Default values: 0 for int, 0.0 for real types, null for reference types.

2D Arrays

A 2D Array is essentially an array of arrays:

int[][] numbers = new int[5][6];

Creates 5 rows and 6 columns. A 2D array can have varying column counts across rows (ragged array).

For-Each Loop

Overview of For-Each Loop:

int[] numbers = {1, 2, 3, 4, 5};
for(int num : numbers) { /* Process each element *\/ }

Note: Limited to read-access of each element.

Varargs

Declaring a Varargs Method:

public static void func(int... a) {}

Varargs parameter must be the last parameter in a method.

ArrayList

Characteristics: Dynamic Size, works with reference types only (requires wrapper classes for primitives).

Declaring an ArrayList

Examples:

ArrayList list = new ArrayList<>();
var list = new ArrayList();

Useful ArrayList Methods:

  • add(e) - Adds element to end of list
  • add(i, e) - Adds element e at index i
  • contains(e) - Checks if element e is in the list
  • get(i) - Retrieves element at index i
  • remove(e) - Removes element e from the list
  • set(i, e) - Sets element at index i
  • size() - Returns the current size of the list

Arrays Class

Import Statement: import java.util.Arrays;

Utility Methods:

Notably, Arrays.copyOf(...);