Chapter 11 Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 11 Arrays

Description:

From time to time, the fact that Java starts index numbering at 0 can be confusing. ... Use Java's index number internally and then add one whenever those numbers are ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 23
Provided by: qiao
Category:
Tags: arrays | chapter | javas

less

Transcript and Presenter's Notes

Title: Chapter 11 Arrays


1
Chapter 11 Arrays
2
Introduction
  • Array An ordered collection of values with two
    distinguishing characters
  • Ordered and fixed length
  • Homogeneous. Every value in the array must be of
    the same type
  • The individual values in an array are called
    elements.
  • The number of elements is called the length of
    the array
  • Each element is identified by its position number
    in the array, which is called index. In Java, the
    index numbers begin with 0.

3
Array declaration
  • An array is characterized by
  • Element type
  • Length
  • type identifier new
    typelength
  • Default values in initialization
  • numerics 0
  • boolean false
  • objects null

4
An array of objects
  • Elements of an array can be objects of any Java
    class.
  • Example An array of 5 instances of the student
    class
  • Student topStudents new Student5

5
Defining length
  • Use named constant to declare the length of an
    array.
  • private static final in N_JUDGES 5
  • double scores new doubleN_JUDGES
  • Or read the length of an array from the user.

6
Selecting elements
  • Identifying an element
  • arrayindex
  • Index can be an expression
  • Cycling through array elements
  • for (int i 0 i lt array.length i)
  • operations involving the ith
    element

7
Human-readable index values
  • From time to time, the fact that Java starts
    index numbering at 0 can be confusing. Sometimes,
    it makes sense to let the user work with index
    numbers that begin with 1.
  • Two standard ways
  • Use Javas index number internally and then add
    one whenever those numbers are presented to the
    user.
  • Use index values beginning at 1 and ignore the
    first (0) element in each array. This strategy
    requires allocating an additional element for
    each array but has the advantage that the
    internal and external index numbers correspond.

8
Internal representation of arrays
  • Student topStudents new Student2
  • topStudents0 new Student(Abcd, 314159)

1000
1004
1008
length
2
FFB8
1000
topStudents
100C
topStudents0
null
FFBC
1010
null
topStudents1
FFC0
stack
heap
9
1000
Student topStudents new Student2
1004
topStudents0 new Student(Abcd, 314159)
1008
2
length
100C
1028
topStudents0
1010
null
topStudents1
1014
1018
101C
4
length
1020
A
b
1024
c
d
1028
102C
1030
1014
studentName
1034
314159
studentID
1038
1000
topStudents
FFB8
creditsEarned
0.0
103C
FFBC
1040
false
FFC0
paidUp
10
Passing arrays as parameters
  • Recall Passing objects (references) versus
    primitive type (values) as parameters.
  • Java defines all arrays as objects, implying that
    the elements of an array are shared between the
    callee and the caller.
  • swapElements(arrayi, arrayn i 1) (wrong)
  • swapElements(array, i, n i 1)

11
  • private void swapElements(int array, int p1,
    int p2)
  • int tmp arrayp1
  • arrayp1 arrayp2
  • arrayp2 tmp
  • Every array in Java has a length field.
  • private void reverseArray(int array)
  • for (int i 0 i lt array.length / 2 i)
  • swapElements(array, i, array.length i 1)

12
Using arrays
  • Example Letter frequency table
  • Design a data structure for the problem
  • Array letterCounts
  • index distance from A
  • index Character.toUpperCase(ch) A
  • letterCounts0 is the count for A or a

13
  • A convenient way of initializing an array
  • int digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • private static final String US_CITIES_OVER_ONE_
    MILLION
  • New York,
  • Los Angeles,
  • Chicago,
  • Huston,
  • Philadelphia,
  • Phoenix,
  • San Diego,
  • San Antonio,
  • Dallas,

14
Arrays and graphics
  • Arrays turn up frequently in graphical
    programming. Any time that you have repeated
    collections of similar objects, an array provides
    a convenient structure for storing them.
  • As an aesthetically pleasing illustration of both
    the use of arrays and the possibility of creating
    dynamic pictures using nothing but straight lines
    the text presents YarnPattern program, which
    simulates the following process
  • Place a set of pegs at regular intervals around a
    rectangular border
  • Tie a piece of colored yarn around the peg in the
    upper left corner
  • Loop that yarn around that peg a certain distance
    DELTA ahead
  • Continuing moving forward DELTA pegs until you
    close the loop

15
Two-dimensional arrays
  • Each element of an array is an array (of the same
    dimension)
  • int A new int32
  • An array of three arrays of dimension two
  • A00 A01
  • A10 A11
  • A20 A20
  • 3-by-2 matrix

16
Memory allocation (row orientation)
A00
A01
A10
A11
A20
A21
17
Initializing a two-dimensional array
  • Static int A32
  • 1, 4,
  • 2, 5,
  • 3, 6
  • A 3-by-2 matrix

18
The ArrayList Class
  • Although arrays are conceptually important as a
    data structure, they are not used as much in Java
    as they are in most other languages. The reason
    is that the java.util package includes a class
    called ArrayList that provides the standard array
    behavior along with other useful operations.
  • ArrayList is a Java class rather than a special
    form in the language. As a result, all
    operations on ArrayLists are indicated using
    method calls. For example,
  • You create a new ArrayList by calling the
    ArrayList constructor.
  • You get the number of elements by calling the
    size method rather than by selecting a length
    field.
  • You use the get and set methods to select
    individual elements.

19
Methods in the ArrayList class
  • Figure 11-12, p. 443, where ltTgt is the base type.
  • boolean add(ltTgt element)
  • ltTgt remove(int index)
  • int indexOf(ltTgt value)
  • An ArrayList allows you to add new elements to
    the end of a list. By contrast, you cant change
    the size of an existing array without allocating
    a new array and then copying all the elements
    from the old array into the new one.

20
Linking objects
  • Objects in Java can contain references to other
    objects. Such objects are said to be linked.
    Linked structures are used quite often in
    programming.
  • An integer list
  • public class IntegerList
  • public IntegerList(int n, IntegerList link)
  • value n
  • next link
  • / Private instance variables /
  • private int value
  • private IntegerList next

21
Linked structures
  • Java defines a special value called null to
    represent a reference to a nonexistent value and
    can be assigned to any variable that holds an
    object reference. Thus you can assign null to the
    next field of the last element to signify the end
    of the list.
  • You can insert or remove an element from a list.
    The size of a linked structure can change. Also,
    elements of a linked structure can be objects.
  • A simple example SignalTower class, Figure 7-3,
    p. 242.

22
Arrays vs. linked lists
  • The two attributes that define a data type are
    domain and a set of operations.
  • An array is a collection of items of the same
    type. It is efficient to select an element. The
    addresses of arrayi is the address of array
    sizeof(overhead) isizeof(type). For example,
    if the type is int, then sizeof(int) is 4. Since
    the array size is fixed, it is hard to insert or
    delete an element.
  • The items on a list can have different types.
    Linked lists can represent general structures
    such as tree. Items can be inserted to or removed
    from a list. However, to select an element, you
    have to follow the links starting from the first
    item on the list (sequential access).
Write a Comment
User Comments (0)
About PowerShow.com