Java Arrays and Array Lists

download Java Arrays and Array Lists

of 22

Transcript of Java Arrays and Array Lists

  • 8/6/2019 Java Arrays and Array Lists

    1/22

    One variable to maintain an entire sequence of values of the same type

  • 8/6/2019 Java Arrays and Array Lists

    2/22

    J ava language has a built-in array structure found in java.lang that is has its own special syntax.

    The array is used to store a list of values of the sametype.

    The values or elements may be a primitive data type

    such as int or double, or a Class objects such asRectangles or Strings.

    Example: one array could contain 10 20 13 33 5 and adifferent array could contain swan pig dog

  • 8/6/2019 Java Arrays and Array Lists

    3/22

  • 8/6/2019 Java Arrays and Array Lists

    4/22

    B efore using the array, your code must use thenew operator to set aside space for the array

    indicating the data type and maximumnumber of values or elements to be stored inthe structure.

    variable-identifier = new data-type [ max-size];double [ ] price; // declaration of listprice = new double [5]; //allocate space

  • 8/6/2019 Java Arrays and Array Lists

    5/22

    A llocation of the array sets all of the arrayelement values to the default for the

    specified data type.double [ ] price; // declaration of listprice = new double [5]; //allocate space

    p0.0 0.0 0.0 0.0

    reference

    price

    price[0] price[1] price[2] price[3] price[4]

    0.0

  • 8/6/2019 Java Arrays and Array Lists

    6/22

    A rray indexing, like the positions of charactersin Strings, starts with O. A rrays allow access

    to any element using its position called index.

    price[0] = 4.95; // stores 4.95 in first price

    4.95 0.0 0.0 0.0

    reference

    price

    price[0] price[1] price[2] price[3] price[4]

    0.0

  • 8/6/2019 Java Arrays and Array Lists

    7/22

    U se the array elements as if they are variablesin their own right.

    price[0] = 4.95; // stores 4.95 in first priceprice[2] = price[0] + 0.50;

    4.95 0.0 5.45 0.0

    reference

    price

    price[0] price[1] price[2] price[3] price[4]

    0.0

  • 8/6/2019 Java Arrays and Array Lists

    8/22

    U se the array-variable.length variable to getthe number of elements. The price.length is 5

    for our price array.for(int index = 0; index < price.length ; index++)

    { price[index] = price[index] + 1.00; }

    5.95 1.0 6.45 1.0

    reference

    price

    price[0] price[1] price[2] price[3] price[4]

    1.0

  • 8/6/2019 Java Arrays and Array Lists

    9/22

    You can declare, allocate and initialize in onestatement.

    int [] test = { 100, 90, 85, 92};

    100 90 85

    reference

    test

    test[0] test[1] test[2] test[3]

    92

  • 8/6/2019 Java Arrays and Array Lists

    10/22

  • 8/6/2019 Java Arrays and Array Lists

    11/22

    U se the .clone() method to create a new arrayof the same size and copy the values into that

    array. Note that you have to cast the clone.int [] test = { 100, 90, 85, 92};int [] twin= (int []) test.clone(); //ref is different

    100 90 85reference2

    twin twin[0] twin[1] twin[2] twin [3]

    92

    100 90 85reference1

    testtest[0] test[1] test[2] test[3]

    92

  • 8/6/2019 Java Arrays and Array Lists

    12/22

    You can use a loop to copy the number of values from onearray (called the source array) into another array (calledthe target array) or use System.arraycopy().

    System.arraycopy(fromIndex, source, toIndex, target, number);

    int [] test = { 100, 90, 85, 92};

    int [] part = new int[5];System.arraycopy( 1, test, 3, part, 2);

    0 0 0reference

    partpart[0] part[1] part[2] part[ 3]

    90

    part[4]

    85

  • 8/6/2019 Java Arrays and Array Lists

    13/22

    You can use a loop to copy the values from the source array into the targetarray using a loop, but it takes a lot more code!

    int [] test = { 100, 90, 85, 92};int [] part = new int[5];int fromIndex = 1; // source array index to copy fromint toIndex = 3; // target array s index to copy toint number = 2; // number of elements to copyfor(int n = 0; n < number; n++){

    part[toIndex] = test[fromIndex];toIndex++;fromIndex++;

    }

  • 8/6/2019 Java Arrays and Array Lists

    14/22

    Rectangle [] boxlist = new Rectangle [3];boxlist[0] = new Rectangle(10, 20, 30, 40);

    boxlist[1] = new Rectangle(50, 30, 10, 20);boxlist[2] = new Rectangle(15, 35, 20, 45);

    reference

    boxlist boxlist[0] boxlist[1] boxlist[2]

    x=10, y=20,w=30,h=40

    x=50, y=30w=10, h=20

    x=15, y=35w=20, h=45

  • 8/6/2019 Java Arrays and Array Lists

    15/22

  • 8/6/2019 Java Arrays and Array Lists

    16/22

    A rrayL ist class manages a sequence of objects (not primitivedata types).The A rrayL ist template specifies the class of objects to bestored in the sequence.The template type is specified in angle brackets < >when theclass variable is declared.A rrayL ist indexes start with 0, just as String and arrays do.

    A rrayL ist words = new A rrayL ist();A rrayL istboxes = new A rrayL ist();

    A rrayL ist accts ;accts = new A rrayL ist();

  • 8/6/2019 Java Arrays and Array Lists

    17/22

  • 8/6/2019 Java Arrays and Array Lists

    18/22

  • 8/6/2019 Java Arrays and Array Lists

    19/22

    void .add(E element) adds element to end of list

    void .add(E element, int index) inserts element into the list at indexposition and moves remaining elements over (re-indexes them).

    boolean .contains(o bject o bj) returns true if element is in list and false,if not.

    E .get(int index) returns the element found at index position

    E .remo ve(int index) removes and returns element at index andmoves remaining elements back (re-ndexes them).

    E .set(int index , E element) replaces the element at index positionwith the specified element and returns old element.

    int .size() returns number of elements in the list

  • 8/6/2019 Java Arrays and Array Lists

    20/22

  • 8/6/2019 Java Arrays and Array Lists

    21/22

    String x = towns.remove(2); // indexed from OSystem.out.println("removed " + x);System.out.println(towns);String y = towns.remove(2);System.out.println( removed + y);System.out.println(towns);

    Output:

    removed L aurens[Hartsville, Columbia, Greenville]removed Greenville[Hartsville, Columbia]

  • 8/6/2019 Java Arrays and Array Lists

    22/22

    A rrayL ist boxlist = new A rrayL ist();boxlist.add(new Rectangle(10, 20, 30, 40) );Rectangle box = new Rectangle(20, 20, 30, 30);boxlist.add(box);boxlist.add(new Rectangle(4, 5, 6, 7));box = new Rectangle(1,1,1,1);boxlist.add(box);System.out.println(boxlist);

    Output:[java.awt.Rectangle[x=10,y=20,width=30,height=40], java.awt.Rectangle[x=20,y=20,width=30,height=30], java.awt.Rectangle[x=4,y=5,width=6,height=7], java.awt.Rectangle[x=1,y=1,width=1,height=1]]