ICS4U Grade 12 – Computer Science – Arrays

Grade 12 – Computer Science

 

Arrays – A Guide to Programming in Java Ch 10

 

Declaring Arrays

  • Arrays: are data structures that can store many of the same kinds of data together at once.
  • Arrays start at index zero and has the declared amount of elements.
  • Array Element Index: the place at which an array element is located
  • Declaring arrays
    • <type> [][] <name> = new <type> [<num1>][<num2>]
    • Square brackets are used to access certain places in arrays
  • Initial Array Values: for int arrays, all initial values are 0. For String arrays, all initial values are null
  • Declaring arrays by initializing elements
    • <type>[] <name> = {<element1>, .. <elementn>};

 

Using Arrays

  • Access elements using the index number in square brackets
    • <name> [<index>]
  • Change elements by making that index equal something
    • <name> [<index>] = <value or a variable>
  • Traversing: using a for loop to go through an entire array and search / print out elements
    • these for loops start at 0 and goes until it’s one less than the length of the array
    • Array Length: can be obtained by <array>.length;
  • For-each statement: this type of loop can do the traversing for you
    • for (<type> element : <array>) {

<statements>;

}

    • The for-each prevents the exception ArrayIndexOutOfBoundsException error
    • This cannot be used if you want to know the index at which a value is located

 

Array Parameters

  • Arrays passed to a method can be entire array or element of the array
    • public static void testMethod( int[] array, int aNum) …
    • Square brackets are used to signify that this is an array
  • Passed arrays are a mere reference to the original data type
  • Passing single elements in an array will not be referenced, the actual value will be passed

 

Arrays with Meaningful Indexes

  • Using arrays you can reference certain indexes as a value then add frequency.
    • For example, in a dice rolling app, the value at index [1] can hold the frequency each time 1 was rolled by the dice.
  • Offset array Indexes: when you are using indexes for huge numbers like frequency of years from 1990 to 2001, it doesn’t make sense to make an array 2001 in size but only use the last 100. So you can offset it by saying 1990 is index 0. To do this, when accessing the array, you simply subtract it by the amount you offset.

 

Characters and Arrays

  • Strings cannot be manipulated as a set of characters, but they can be converted into a char array where each letter is stored in it’s own index.
  • the charAt(int index) method can return the char value at the letter position index.
  • toCharArray() method converts each character in the string to char and places it in the char array.
  • Unicode: each letter and symbol is assigned a digital representation called Unicode.
    • 16 ones and zeros are used for each letter.
    • A letter in the char variable is stored as Unicode representation of the letter
    • Lower case letters have higher values in Unicode than upper case letters.
    • You can type cast char to any number and it will return the Unicode equivalent of that character.

 

Searching an Array

  • Linear Search: will look at one array element at a time until a specific value is found or after entire array is searched.
    • If the element is found, the search will stop at that spot

 

2D Arrays

  • 2D arrays: can store a table of information with 2 dimensions
  • Declare them using 2 square brackets
    • <type> [][] <name> = new <type> [<num>][<num>];
  • Access 2D arrays using their position in the square brackets
  • The coordinates follow X Y coordinates and index starts at zero
  • To find length, you do:
    • array.length;   to find the length for the horizontal row
    • array[0].length;   to find the length for the vertical column

 

ArrayList Class

  • ArrayList is a collection of related objects stored as a single unit.
    • Java has a collections framework and ArrayList is one example of such framework
    • ArrayList Class includes methods for adding, deleting, or finding elements
  • add(int index, element); : inserts element at index position and moves everything to the next position in the array
  • add(element); :inserts the element at the end of the array
  • get(int index); :returns the element at index position
  • indexOf(obj); :returns the index of the first element matching obj using the equals() method of the object’s class to determine equality between the elements
  • remove(int index); :removes the element at index position in the array and moves everything up one position
  • set(int index, element); :replaces the element at index with element.
  • size(); :returns the number of elements in the array
  • Dynamic Array: ArrayList is a dynamic array because elements can be added and grow or shrink as the applications needs it.
  • The equals() method is used in the indexOf() ArrayList method because objects are stored in ArrayList, not primitive types
  • Declaring ArrayList
    • ArrayList<String> name = new ArrayList<String>();
    • Type Parameter: the <String> part tells the compiler that ArrayList contains String objects.

 

Wrapper Classes

  • Since primitive data types cannot be stored in ArrayList, Integers or Doubles must be wrapped into objects before they can be compared.
  • Similarly, they must be converted back to primitive data types when retrieving arrays
  • To do that, we use Wrapper Classes in java.lang.Integer or java.lang.Double.

 

  • compareTo(Integer intObject); : returns 0 when the integer object value is the same as intObject. A negative int is returned when the Integer object is less than intObject and a positive int is returned when the Integer object is greater than intObject.
  • intValue(); :returns the int value of the Integer object.

 

  • To take advantage of these classes, the ArrayList type parameter must be Integer or Double.

 

AutoBoxing and AutoUnboxing

  • Using wrapper classes to wrap and unwrap elements can be cumbersome and make codes extremely long
  • Java includes autoboxing and autounboxing.
  • These will eliminate the need for extra code, once the ArrayList is set to the right type parameter, autoboxing and autounboxing will take care of the wrapping for you.