Array Basics

Lecture 2012-01-25

CS II Array Basics

Java Arrays

  • Once created, the number of items held is constant.
  • You CANNNOT resize an array; it is a fixed space in memory.
    • well, you can, but you have to declare a new array and copy the data over.

The array length

  • In java, arrays really are objects

0 1 2 3 4 5 6 | | | | | | | | ^ 8 bytes

Arrays and ==

  • Arrays are objects in Java
    • == compares to see if two arrays are the same object, rather than comparing their contents.
  • Equals method (A.equals(B))
    • A and B contain the same elements in the same order.
  • Arrays are References

Java's Built-in Array Sorting

  • Arrays.sort(anArray);
  • Arrays.sort(anArray, first, last);

Creating a list with an array

blarg

StringList functionality

public class StringList {
    private String[] str;
    private int size;

    public StringList() {
        str = new String[1];
        size = 0;
    }
}

Applets