Skip to main content

cs2381 Notes: 11 Arrays

··1 min

Introducing Arrays

Today, we’re going to look at a new kind of object in Java.

It’s not a primitive value type. It’s not a class instance object. It’s an array of objects.

Facts about Arrays:

  • A collection of zero or more items.
  • All items are the same type.
  • Each array has a fixed length, specified when it is created.
  • Items in the array can be accessed (and modified) by numerical index, starting at 0.
  • Arrays have a length field that can be accessed directly.

Simple example:

    int[] xs = new int[] = {1, 2, 4};
    int[] ys = new int[3];
    
    ys[1] = xs[0] + xs[1];
    
    System.out.println("" + ys[1]); // 3

The pattern to manipulate an array is a for loop over an int.

    int[] xs = new int[] = {1, 2, 4};

    int sum = 0;
    for (int ii = 0; ii < xs.length; ++ii) {
        sum += xs[ii];
    }
    
    System.out.println("sum = " + sum);

ArrayWrapper

Add some methods:

  • get(ii)
  • put(ii, vv)
  • resize(nn)
  • pushFront(vv)
  • pushBack(vv)

ListWrapper

Same methods.

Arrays vs Cons Lists

Operation List Array
Get first item O(1) O(1)
Get item by index i O(n) O(1)
Set item at index i O(n) O(1)
Add item to front O(1) O(n) (including resize)
Insert item after index ii O(n) O(n) (including resize)