Site Search:

Declare, instantiate, initialize and use a one-dimensional array

Back OCAJP















An array is an ordered list with fixed size.
The most common ways to create an array are:

type[] variableName = new type[size];
type variableName[] = new type[size];

Array have fixed size, so you have to specify array size or give initial values (which specifies size implicitly). When you don't give initial values, the default values are given. Though Array's size can not change, array's contents can change, so array is mutable.
======

OCAJP>cat test.java 
class test {
  public static void main(String[] args) {
    String[] stringArr = new String[9];
    int[] intArr = new int[3];  //initial value is 0,0,0, size is 3
    //boolean[] booArr = new boolean[];  //array dimension missing
    //String foo[] = new foo[2];  //array name is not a type
    boolean[] booleanArr = new boolean[5];
    Boolean    [] booArr1 = new Boolean[2];  //null, null, size is 2
    Boolean booArr2 [] = new Boolean[4];
    Boolean booArr3[] = new Boolean[2];  //false, false, size is 2

    //initialize
    int intArr2[] = new int[] {1,2,3};   //initial value is 1, 2, 3, size is 3
    int[] intArr4 = new int[]{0,9,2};
    int[] intArr1 = {1,3,5,7}; 
    //char[] charArr = new Char[] {'a', 'b'};  //Char can not find symbol
    //int intArr3  [] = new int[2]{1,2};  //not a statement
    for(int i: intArr4) {
      System.out.print(i+" ");
    }
    System.out.println();
  }
}
OCAJP>javac test.java 
OCAJP>java test
0 9 2 


Array is an object (although weird looking). That means array can be initialized with new method, it extends object class, and has methods.

For example, while int i; is a primitive type variable stores a number, int[] b; is an array reference variables that points to an array of int object or null.


OCAJP>cat test.java 
class test {
  public static void main(String[] args) {
    Integer[] a = new Integer[]{1, 3, 4};
    for(int i:a)System.out.print(i+"  ");
    Integer[] b = a;  //now both a, b refer to the same array object
    b[0] = 10;  //change object through b
    System.out.println();
    for(int i:a)System.out.print(i+"  ");
    System.out.println(a.equals(b));  //true
    System.out.println(a == b);  //true

    Integer c[] = b.clone();  //c refer to a different array object
    for(int i:c)System.out.print(i+"  ");
    for(int i:b)System.out.print(i+"  "); 
    System.out.print("c==b: ");  
    System.out.println(c==b);  //false
    System.out.print("c.equals(b): ");
    System.out.println(c.equals(b));  //false
    c[2] = 9;
    for(int i:c)System.out.print(i+"  ");
    for(int i:b)System.out.print(i+"  ");  //b didn't change
    System.out.println(c.equals(b));  //false

    b[2] = 9;
    for(int i:a)System.out.print(i+"  ");
    System.out.print("equals: ");System.out.println(c.equals(a)); //false
    System.out.print("== compare referenced object:"); System.out.println(c==a);  //false
    
    //Long[] c = b; //incompatible types: Integer[] cannot be converted to Long[]
    //Short d[] = b;  //incompatible types: Integer[] cannot be converted to Short[]
    //int[] c = b;  //incompatible types: Integer[] cannot be converted to int[]
  }
}
OCAJP>javac test.java 
OCAJP>java test
1  3  4  
10  3  4  true
true
10  3  4  10  3  4  c==b: false
c.equals(b): false
10  3  9  10  3  4  false
10  3  9  equals: false
== compare referenced object:false


Besides methods inherited from object class, array class only have one method: length, which returns the size of the array. We can copy, compare, sort, search etc. using static methods of java.util.Arrays class.


OCAJP>cat test.java 
import java.util.*;
class test {
  public static void main(String[] args) {
    String[] a = new String[] {"cat", "dog", "bird", "9", "10"};
    String[] b = new String[] {"cat", "dog", "bird", "9","10"};
    //String c [] = Arrays.copyOf(b);  //no suitable method found for copyOf(String[])
    System.out.println(Arrays.equals(a,b));  //compare contents
    System.out.println(a.equals(b));  //array didn't override Object class's equals method
    for(String s: a) System.out.println(s);
    Arrays.sort(a);  //sort by alphabetic order
    for(String s: a) System.out.println(s);
    System.out.println(Arrays.binarySearch(a, "dog"));  //sorted
    System.out.println(Arrays.binarySearch(b, "dog"));  //unsorted, unpredicatable result
    System.out.println(Arrays.binarySearch(a, "nosuchthing"));  //sorted
    System.out.println(Arrays.binarySearch(b, "nosuchthing"));  //unsorted, unpredicatable result
  }
}
OCAJP>javac test.java 
OCAJP>java test
true
false
cat
dog
bird
9
10
10
9
bird
cat
dog
4
-6
-6
-6

In the above example, 
System.out.println(Arrays.binarySearch(a, "nosuchthing"));
print out -6.
How it is calculated?
Arrays.sort(a);  //sort by alphabetic order
a became {"10", "9", "bird", "cat", "dog"}.
if nosuchthing is added into, the array will be {"10", "9", "bird", "cat", "dog", "nosuchthing"}.
The index of "nosuchthing" is 5.
negate the index then minus 1 we got -6.

Finally, let's take a look at some OCA tricks.

OCAJP>cat test.java 
import java.util.*;
class test {
  public static void main(String... args) {
    int a, b = 3, c[];
    c = new int[3];
    //a = new int[3]; //not compile
    a = 2;

    int[] d, e, f = {2,3}, g;
    d = e = f;
    List<Integer> m = Arrays.asList(f);
    //List<Integer> m = f.asList();  //not comile
    //d = 2; //not compile
    //g = args;  //not compile    
    String[] h = args;
    h[0] = "change parameter";
    //System.out.println(h.capacity);  //not compile
    //System.out.println(h.capacity());  //not compile
    for(String arg: h) System.out.println(arg);
    args = null;
    for(String arg: h) System.out.println(arg);
  }
}
OCAJP>javac test.java 
OCAJP>java test a b c
change parameter
b
c
change parameter
b
c
OCAJP>java test
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at test.main(test.java:14)


Back OCAJP