OneDimensional Arrays - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

OneDimensional Arrays

Description:

Sami. Array elements are handles for String values. 12/18/09. ICS102: 1-D Arrays. 16 ... Sami. 12/18/09. ICS102: 1-D Arrays. 20. Array Cloning with Reference Types ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 24
Provided by: Jiawe7
Category:

less

Transcript and Presenter's Notes

Title: OneDimensional Arrays


1
One-Dimensional Arrays
2
Arrays
  • An array is an ordered list of values
  • An array of size N is indexed from zero to N-1
  • This array holds 4 values that are indexed from 0
    to 3

Entire array has A single name
Each value has A numeric index
3
Processing Array Elements
  • Access individual elements of an array using
  • the name of the array
  • a number (index or subscript) that tells which of
    the element of the array
  • What value is stored in scores2?

4
Arrays
  • For example, an array element can be assigned a
    value, printed, or used in a calculation
  • scores2 87
  • scoresfirst scoresfirst 2
  • Avg (scores0 scores1)/2
  • System.out.println (Max " scores3)

5
Arrays
  • The values held in an array are called array
    elements
  • Array elements are of the same type
  • The element type can be a primitive type or an
    object reference.
  • Therefore, we can create an array of integers, or
    an array of characters, or an array of String
    objects, etc.
  • In Java, the array itself is an object
  • Therefore the name of the array is a object
    reference variable, and the array itself must be
    instantiated

6
Declaring an Array
  • This declares the handle only
  • Initialized to null
  • Stores an address when arrays are created
  • Defining an array
  • Type name
  • Where
  • Type specifies the kind of values the array
    stores
  • the brackets indicate this is an array
  • name is the handle to access the array

7
Declaring an Array
  • The scores array could be declared as follows
  • double scores new double 4
  • The above statement will
  • allocate block of memory to hold 4 doubles
  • initialize block with zeros
  • create a handle or a pointer or a reference
    called scores
  • store address of the block in scores

0.0
0.0
0.0
0.0
Block
Handle
8
Syntax
  • Forms
  • ElementType arrayNameElementType
    arrayName new ElementType sizeElementType
    arrayName array-literal
  • Where
  • ElementType is any type
  • arrayName is the handle for the array
  • array-literal is a list of literals enclosed in
    curly braces

9
Declaring Arrays
  • Some examples of array declarations
  • float prices new float500
  • boolean flags
  • flags new boolean20
  • char codes new char1750

10
Bounds Checking
  • Once an array is created, it has a fixed size
  • An index used in an array reference must specify
    a valid element
  • That is, the index value must be in bounds (0 to
    N-1)
  • The Java interpreter throws an ArrayIndexOutOfBoun
    dsException if an array index is out of bounds
  • This is called automatic bounds checking

11
Bounds Checking
  • For example, if the array scores can hold 100
    values, it can be indexed using only the numbers
    0 to 99
  • If scores has the value 100, then the following
    reference will cause an exception to be thrown
  • System.out.println (scorescount)
  • Often for loop is used to process array. Its
    common to introduce off-by-one errors when using
    arrays
  • for (int index0 index lt 100 index)
  • scoresindex index50 epsilon

problem
12
Bounds Checking
  • Each array object has a public constant called
    length that stores the size of the array
  • It is referenced using the array name
  • scores.length
  • Note that length holds the number of elements,
    not the largest index

13
Initializer Lists
  • An initializer list can be used to instantiate
    and initialize an array in one step
  • The values are delimited by braces and separated
    by commas
  • Examples
  • int scores 98, 76, 54, 83, 87, 65, 99,
    66
  • char letterGrades 'A', 'B', 'C', 'D',
    F'

14
Initializer Lists Example 1
  • Used when exact size and initial values of an
    array are known in advance
  • int scores 98, 76, 54, 83
  • Visualize the results of the above command

15
Initializer Lists Example 2
  • Consider the following 4 student names
  • Note results

String STUDENTS Aref", Ali, Emad,
"Sami"
Aref
Ali
Emad
Sami
Array elements are handles for String values
16
Initializer Lists
  • Note that when an initializer list is used
  • the new operator is not used
  • no size value is specified
  • The size of the array is determined by the number
    of items in the initializer list
  • An initializer list can only be used only in the
    array declaration

17
The Assignment Operation
  • Java provides a few operations to use with
    arrays, including assignment
  • Consider
  • int alist 1, 12, 15, 7
  • int blist
  • blist alist
  • Recall that alist and blist are handles
  • alist contains an address of where the numbers
    are
  • blist now contains that same address
  • blist does not have a copy of alist

18
Array Cloning
  • To actually create another array with its own
    values, Java provides the .clone() method
  • int alist 1, 12, 15, 7
  • int blist
  • blist alist.clone()
  • Now there are two separate lists of numbers, one
    with handle alist, the other with handle blist

19
Array Cloning with Reference Types
  • Recall previous declaration
  • Consider
  • String s_list STUDENTS.clone()
  • This will create another list of handles, also
    pointing to the names

Aref
Ali
Emad
Sami
20
Array Cloning with Reference Types
  • We can write our own "deep copy"
  • String original Aref,Ali, Emad,
    Sami
  • String result new Stringoriginal.length
    for (i 0 i lt original.length i)
    resulti originali.clone()

21
Array Equality
  • Java has an equals() method for classes
  • if (a1.equals(a2))
  • If a1 and a2 are arrays, the equals() method just
    looks at the addresses the handles point to
  • They could be pointing to different addresses but
    the contents of the array still be equal
  • It is the contents that we really wish to compare

22
Array Equality
  • We must write our own method to compare the
    arrays
  • they both must have the same length
  • then use a for( ) loop to compare element by
    element for equality
  • Boolean areEqual true
  • if (list1.length ! list2.length) areEqual
    false
  • else
  • for (int i 0 i lt list1.length i) if
    (list1i ! list2i) areEqual false
  • if(areEqual) System.out.println(They are
    equal)
  • else System.out.println(They are NOT equal)

23
THE END
Write a Comment
User Comments (0)
About PowerShow.com