159'234 OOP Lecture 30 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

159'234 OOP Lecture 30

Description:

The two arrays are created but their contents are not. people contains 50 references, each to an object of type Person, whose value is null. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 15
Provided by: ITS8213
Category:
Tags: oop | compile | lecture

less

Transcript and Presenter's Notes

Title: 159'234 OOP Lecture 30


1
159.234 OOP - Lecture 30
  • Java Arrays
  • Multidimensional Arrays
  • Copying arrays

2
5. Java Arrays
  • Declaring and Creating Arrays
  • Initialising Arrays
  • Multi-dimensional Arrays
  • Arrays are Objects
  • Copying Arrays

3
  • Arrays can be declared of any type fundamental
    or class
  • char a
  • Person people
  • The above declarations just declare the array
    reference -- not the objects in the array.
  • Two forms of array declaration
  • char firstName
  • Person people
  • Both forms are equivalent. With the latter you
    may consider the declaration as having the type
    on the left and the variable name on the right.
  • Create arrays by using the new keyword
  • firstName new char40
  • people new Person50
  • The two arrays are created but their contents are
    not. people contains 50 references, each to an
    object of type Person, whose value is null. Each
    Person object must be explicitly created
  • people0 new Person()
  • people1 new Person()
  • ...

4
  • Java supports multi-dimensional arrays through
    the creation of arrays of arrays.
  • Java, like C, is row-major.
  • Rectangular arrays are simple to declare
  • int chessBoard new int88
  • int checkerBoard new int88
  • Java allows a shorthand way of initialising
    arrays if you know exactly how many elements the
    array will hold and what their initial values
    will be.
  • String greetings
  • "Zeroth String",
  • "First String",
  • "Second String",
  • "Last String"
  • The only difficulty is that you can't use this
    method to create a large array and only
    initialise selected elements.
  • One way to do this is in a static block -- later
    in the course.

5
  • Non-rectangular arrays are also supported. Care
    must be taken to ensure all elements are
    initialised.
  • float bigMatrix new float100
  • bigMatrix0 new float50
  • bigMatrix1 new float12
  • bigMatrix2 new float123
  • bigMatrix is a two-dimensional array with 100
    rows and an undefined number of columns.
  • After the initialisation is complete
  • column 0 has capacity for 50 elements
  • column 1 has capacity for 12
  • column 2 has capacity for 123
  • the remainder of the rows are still initialised
    to null.

6
  • Arrays are stored as full objects in Java.
  • The array variable is really a reference to the
    array object.
  • The number of elements in an array is stored as
    part of the array object.
  • This number is used to check for exceptions ie
    ArrayIndexOutOfBounds at runtime.
  • The size of an array can be accessed at runtime
    using the length member variable.
  • Reminder - in Java all array subscripts start
    from zero.
  • int myArray new int100
  • System.out.println( Size of myArray is
    myArray.length)
  • for (int index0 indexltmyArray.length index)
  • // initialise the array contents
  • The last array element is myArray.length - 1

7
  • Copying Arrays
  • Once created, the size of an array is fixed.
  • Arrays can be redeclared.
  • If you use the same variable name (reference
    variable) the old array will be lost.
  • char firstName new char40
  • firstName new char80
  • Java has an inbuilt library class for copying
    arrays efficiently.
  • System.arraycopy()
  • Source and destination arrays need to be the same
    shape.
  • The copy Destination needs to be large enough to
    hold the required number of elements else an
    ArrayIndexOutOfBounds exception will be thrown.
  • java.util.Vector class is often a good
    replacement for an array when the size needs to
    be dynamic.

8
  • public class Arrays
  • public static void main( String args )
  • double x //
    array declaration
  • double b new double3 //
    declaration instantiation
  • double v, y //
    alternative notation
  • double A, H //
    multi dimensional cases
  • double M new double23 //
    are arrays of arrays..
  • double W new double2 //
    2 is not allowed
  • double grid // no
    limit to dimensions
  • //
    (except memory)
  • double fourspace new
    double2222
  • double u 4.0, 5.0, 6.0, 7.0, 8.0
  • x new double3
  • x0 1.0 x1 2.0 x2 3.0 //
    subscripts 0...n-1
  • System.arraycopy(x, 0, b, 0, x.length )//
    efficient copy utility
  • // args are(source, start,
    target, start, length)
  • for(int i0iltb.lengthi)System.out.print(b
    i " ")
  • Example Output
  • gt java Arrays
  • 1.0 2.0 3.0
  • 4.0 5.0 6.0 7.0 8.0
  • M.length is 2
  • M0.length is 3
  • gt

9
5. Exercises
  • Create a new class called IntArray. In the main()
    method declare two variables called firstInt and
    secondInt. They should be of type array-of-int.
  • Create an array of 20 int values. Assign the
    reference to firstInt.
  • Create an array of 20 int values. Initialise the
    array to
  • values in the range of 21 to 40. Assign the
    reference to secondInt. Use the shorthand array
    declaration / initialisation technique.
  • Use a for loop to print out all values of
    firstInt and secondInt. How should you determine
    the number of elements in the arrays?
  • Compile and run your program. What are the
    results?

10
  • Using the code you wrote for calculating the
    partial sum of an array, use this code to store
    the partial sum of the secondInt array in
    firstInt.
  • Print out the contents of both arrays again.
  • Compile and run your program. What are the
    results?
  • Copy the contents of secondInt into firstInt
    using the System.arraycopy() method.
  • Print out the contents of both arrays again.
  • Compile and run your program. What are the
    results?

11
  • Using the Person class developed earlier, modify
    the main() method to declare and create an array
    called people containing 10 Persons.
  • Provide values for the name, address and birth
    date of at least three array elements.
  • Print out the contents of the people array.
  • Compile and run your program to ensure your
    solution is correct.

12
  • Create a class called TwoD. Inside the main()
    method declare a variable called my2DArray which
    is an array of array of int. It should have five
    arrays of ints. Declare a variable called
    my1DArray which is an array of int
  • Define my1DArray to have six elements, containing
    the numbers 0 to 5.
  • Assign this array to element 0 of my2DArray.
  • Write a for() loop to print out the contents of
    the my2DArray to the screen, in matrix format.
  • Compile and run your program. What happens when
    it tries to print out the second (and subsequent)
    row?

13
  • Create more arrays containing different numbers
    of elements assign them to the elements of
    my2DArray.
  • Print out the contents of the my2DArray again.
  • Compile and run your program.
  • Write a program that can check to see if a word,
    represented as a String, is a palindrome (reads
    the same front-to-back as back-to-front, for
    example glenelg).

14
Array Summary
  • Java uses Objects to implement arrays
  • Array references are declared as normal
  • Array objects are created dynamically using new
  • Java uses row-major index ordering
  • Since arrays can contain array references,
    multidimensional arrays and non-rectangular
    arrays are possible.
Write a Comment
User Comments (0)
About PowerShow.com