04 Java Arrays[1]

Embed Size (px)

Citation preview

  • 8/9/2019 04 Java Arrays[1]

    1/16

    CS 335 Lecture 04

    Java Programming Arrays

    Fall 2003

  • 8/9/2019 04 Java Arrays[1]

    2/16

    Arrays in Java

    Indexes start at 0

    All data types (primitive and user-defined

    objects) can be put into arrays Any expression which evaluates to an integer

    can be used as the array subscript

    Arrays can be re-allocated to change the

    size; they are static once created

  • 8/9/2019 04 Java Arrays[1]

    3/16

    Declaring Arrays

    Arrays must be both declaredand allocated(using the new command):

    int myarray[] = new int [100]

    int myarray[] = new int [100]

    int myarray[];myarray = new int[100];

    int myarray[];myarray = new int[100];

  • 8/9/2019 04 Java Arrays[1]

    4/16

    Example Declarations

    char chararray[] = new char [255]byte bytearray[] = new byte [255]

    MyObj ObjArray[];ObjArray = new MyObj[10]

    byte [] array1, array2;array1 = new byte[100];

    array2 = new byte[200];

  • 8/9/2019 04 Java Arrays[1]

    5/16

    Allocating Arrays

    Must allocate with the new command

    null pointer exception!!

    Fixed size after allocation The allocated size can be dynamically

    determined with an expression:

    char chararray[];...chararray = new char [size*6];

    char chararray[];...

    chararray = new char [size*6];

  • 8/9/2019 04 Java Arrays[1]

    6/16

    Dynamic Arrays

    Re-allocation possible:

    char myarray[] = new char [255];char tmparray[] = new char [255];...

    for (int i=0; i

  • 8/9/2019 04 Java Arrays[1]

    7/16

    // Fig. 7.4: InitArray.java// initializing an array with a declarationimport javax.swing.*;

    public class InitArray {public static void main( String args[] ) {

    String output = "";// Initializer list specifies number of elements and// value for each element.int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };

    output += "Subscript\tValue\n";

    for ( int i = 0; i < n.length; i++ )output += i + "\t" + n[ i ] + "\n";

    JTextArea outputArea = new JTextArea( 11, 10 );

    outputArea.setText( output );

    JOptionPane.showMessageDialog( null, outputArea,"Initializing an Array with a Declaration",JOptionPane.INFORMATION_MESSAGE );

    System.exit( 0 ); }}

    // Fig. 7.4: InitArray.java// initializing an array with a declarationimport javax.swing.*;

    public class InitArray {public static void main( String args[] ) {

    String output = "";// Initializer list specifies number of elements and

    // value for each element.int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };

    output += "Subscript\tValue\n";

    for ( int i = 0; i < n.length; i++ )output += i + "\t" + n[ i ] + "\n";

    JTextArea outputArea = new JTextArea( 11, 10 );outputArea.setText( output );

    JOptionPane.showMessageDialog( null, outputArea,"Initializing an Array with a Declaration",JOptionPane.INFORMATION_MESSAGE );

    System.exit( 0 ); }}

  • 8/9/2019 04 Java Arrays[1]

    8/16

    // Fig. 7.5: InitArray.java// initialize array n to the even integers from 2 to 20import javax.swing.*;

    public class InitArray {public static void main( String args[] ) {

    final int ARRAY_SIZE = 10;int n[]; // reference to int arrayString output = "";

    n = new int[ ARRAY_SIZE ]; // allocate array// Set the values in the arrayfor ( int i = 0; i < n.length; i++ )

    n[ i ] = 2 + 2 * i;

    output += "Subscript\tValue\n";

    for ( int i = 0; i < n.length; i++ )output += i + "\t" + n[ i ] + "\n";

    JTextArea outputArea = new JTextArea( 11, 10 );outputArea.setText( output );

    JOptionPane.showMessageDialog( null, outputArea,"Initializing to Even Numbers from 2 to 20",

    JOptionPane.INFORMATION_MESSAGE );

    System.exit( 0 ); }}

    // Fig. 7.5: InitArray.java// initialize array n to the even integers from 2 to 20import javax.swing.*;

    public class InitArray {public static void main( String args[] ) {

    final int ARRAY_SIZE = 10;int n[]; // reference to int arrayString output = "";

    n = new int[ ARRAY_SIZE ]; // allocate array// Set the values in the arrayfor ( int i = 0; i < n.length; i++ )

    n[ i ] = 2 + 2 * i;

    output += "Subscript\tValue\n";

    for ( int i = 0; i < n.length; i++ )output += i + "\t" + n[ i ] + "\n";

    JTextArea outputArea = new JTextArea( 11, 10 );outputArea.setText( output );

    JOptionPane.showMessageDialog( null, outputArea,"Initializing to Even Numbers from 2 to 20",JOptionPane.INFORMATION_MESSAGE );

    System.exit( 0 ); }}

  • 8/9/2019 04 Java Arrays[1]

    9/16

    // Fig. 7.8: Histogram.java// Histogram printing programimport javax.swing.*;

    public class Histogram {public static void main( String args[] ){

    int n[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };String output = "";

    output += "Element\tValue\tHistogram";

    for ( int i = 0; i < n.length; i++ ) {output += "\n" + i + "\t" + n[ i ] + "\t";

    for ( int j = 1; j

  • 8/9/2019 04 Java Arrays[1]

    10/16

    Arrays as Parameters

    Java treats arrays as objects. When the

    actual parameter is an array, it is passed to

    the method by reference:int a[] = { 1, 2, 3, 4, 5};...

    modifyArray ( a );...public void modifyArray (int b[]) {...}

  • 8/9/2019 04 Java Arrays[1]

    11/16

    Arrays as Parameters

    When elements in an array are primitive data

    types, the elements can be passed by value:

    int a[] = { 1, 2, 3, 4, 5};...

    modifyElement ( a[4] );...

    public void modifyElement (int elem) {

    ...}

    int a[] = { 1, 2, 3, 4, 5};...

    modifyElement ( a[4] );...

    public void modifyElement (int elem) {

    ...}

  • 8/9/2019 04 Java Arrays[1]

    12/16

    Example: Binary Search// Binary searchpublic int binarySearch( int array[], int key ){

    int low = 0; // low subscriptint high = array.length - 1; // high subscriptint middle; // middle subscript

    while ( low

  • 8/9/2019 04 Java Arrays[1]

    13/16

    Multi-Dimensional Arrays

    Java supports single arrays whose elements

    are also single arrays, thus achieving the

    effect of double-subscripted arrays:

    int my2darray[][] = { {1, 2}, {3, 4}};

    exprResult = my2darray[0][0]*my2darray[1][0];

    int my2darray[][] = { {1, 2}, {3, 4}};

    exprResult = my2darray[0][0]*my2darray[1][0];

  • 8/9/2019 04 Java Arrays[1]

    14/16

  • 8/9/2019 04 Java Arrays[1]

    15/16

    Example Game: Memory

    Basic constructs: arrays, buttons, objects

    Simple GUI http://www.lizardpoint.com/fun/java/conc/Memory.html

  • 8/9/2019 04 Java Arrays[1]

    16/16