Arrays - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Arrays

Description:

An entire array can be used as a single argument passed to a method. example ... a method for adding an item to the end of the list unless it is on the list already ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 51
Provided by: robertp6
Category:
Tags: arrays

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • Chapter 6

2
The Very Large Array
3
The Very Large Array
  • National Radio Astronomy Observatory, NM
  • The array consists of 27 independent radio
    antennas
  • The array acts like a single antenna with a
    diameter of 36km.
  • The antennas are arranged along three arms of
    Y-shape (each of which measures 21km)
  • The antennas can be physically relocated on the
    railroad tracks along the Y-shape.
  • Stands at 2124m above sea level
  • The dish diameter is 25m and it weighs 230 tons.

4
The Very Large Array
5
Introduction to Arrays
  • An array is an object used to store a (possibly
    large) collection of data.
  • All the data stored in the array must be of the
    same type.
  • An array object has a small number of predefined
    methods.

6
Introduction to Arrays, cont.
  • Sometimes you want to know several things about a
    collection of data
  • the average
  • the number of items below the average
  • the number of items above the average
  • etc.
  • This requires all the items to be stored as
    variables of one kind or another.

7
Creating and Accessing Arrays
  • example
  • double temperature new double7
  • temperature3 32.0
  • temperature6 temperature3 5
  • System.out.println(temperature6)
  • temperatureindex1 66.5
  • The integer expression within the square brackets
    is called the index (or subscript).

8
Creating and Accessing Arrays
9
(No Transcript)
10
Array Base Type
  • The type of the elements is called the base type.
  • The base type of an array can be any type
    including a class type.
  • for example,
  • Species entry new Species3
  • The number of elements is the length or size of
    the array.

11
The length Instance Variable
  • An array has only one public instance variable,
    namely length.
  • The length variable stores the number of elements
    the array can hold.
  • Using Array_Name.length typically produces
    clearer code than using an integer literal.

12
Indices and length
  • The indices of an array start with 0 and end with
    Array_Name.length-1.
  • example
  • for (lcv 0 lcv lt temperature.length
    index)
  • Otherwise, the index is said to be out of bounds
    or invalid.

13
Initializing Arrays
  • double reading 3,3, 15.8, 9.7
  • The size of the array is determined by the number
    of values in the initializer list.
  • int count new int100
  • for (int i 0, i lt count.length, i)
  • ai 0

14
Arrays in Classes and Methods
  • Arrays can be used as instance variables in
    classes.
  • Both an indexed variable of an array and an
    entire array can be a argument of a method.
  • Methods can return an indexed variable of an
    array or an entire array.

15
Case Study Array as an Instance Variable
  • Prepare a sales report showing
  • which sales associate (or associates) has the
    highest sales
  • how each associates sales compare to the
    average.
  • needed for each associate
  • name
  • sales figures

16
Case Study Using an Array as an Instance
Variable, cont.
  • tasks
  • obtain the data (getFigures)
  • update the instance variables (update)
  • display the results (displayResults)

17
Pseudocode
  • An outline of a program, written in a form that
    can easily be converted into real programming
    statements.
  • Pseudocode cannot be compiled executed.
  • There are no real formatting or syntax rules.
  • It is simply one step - an important one - in
    producing the final code.
  • It enables the programmer to concentrate on the
    algorithm without worrying about all the
    syntactic details of a particular programming
    language.

18
Pseudocode - an Example
  • pseudocode for a bubble sort routine
  • while not at end of list compare adjacent
    elementsif second is greater than first
    switch them get next two elementsif elements
    were switched repeat for entire list

19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
Indexed Variables as Method Arguments
24
Arrays as Method Arguments
  • An entire array can be used as a single argument
    passed to a method.
  • example
  • double a new double10
  • SampleClass.change(a)
  • ...
  • public static void change(double d)
  • No brackets accompany the argument.
  • Method change accepts an array of any size.

25
Arguments for the Method main
  • Recall the heading for method main
  • public static void main(String args)
  • Method main takes an array of String values as
    its argument.
  • java TestProgram Mary Lou
  • args0 is set to Mary
  • args1 is set to Lou

26
Use of and with Arrays
  • The assignment operator and the equality
    operator , when used with arrays, behave the
    same as when used with other objects.
  • The assignment operator creates an alias, not a
    copy of the array.
  • The equality operator determines if two
    references contain the same memory address, not
    if two arrays contain the same values.

27
Making a Copy of an Array
  • example
  • int a new int50
  • int b new int50
  • ...
  • for (int j 0 j lt a.length j)
  • bj aj

28
Determining the Equality of Two Arrays
  • To determine if two arrays at different memory
    locations contain the same elements in the same
    order, define an equals method which determines
    if
  • both arrays have the same number of elements
  • each element in the first array is the same as
    the corresponding element in the second array.

29
  • class TestEquals

30
  • class ReturnArrayDemo

31
Programming Example A Specialized List Class
  • An array can be an instance variable of a class
    that is accessible only through the class
    methods.
  • We will define a class whose objects store lists
    of items, such as a grocery list or a list of
    things to do.
  • Well name the class OneWayNoRepeatsList.

32
Programming Example, cont.
  • some features of the class
  • a method for adding an item to the end of the
    list unless it is on the list already
  • an array of strings to hold the items
  • numbering starting with 1 rather than 0
  • a maximum list size
  • accessor and mutator methods, but no methods for
    changing or deleting items
  • a method to erase the entire list

A well-encapsulated class
33
  • class ListDemo

34
More Features of the Class
  • We need a means of determining that the end of
    the list has been reached.
  • Let toDoList.getEntryAt(position) return the
    value null.

35
  • class OneWayNoRepeatsList

36
  • class OneWayNoRepeatsList, contd.

37
Partially Filled Arrays
38
Sorting Arrays
  • Sometime we want numbers in an array sorted from
    smallest to largest, or from largest to smallest.
  • Sometimes we want the strings referenced by an
    array to be in alphabetical order.
  • Sorting techniques typically are easy to adapt to
    sort any type that can be ordered.

39
Selection Sort
40
  • class SelectionSort

41
Sorting Algorithms
  • Heap Sort
  • Bubble Sort
  • Quick Sort
  • .

42
Bubble Sort
Programming Project 4
43
Multidimensional Arrays
44
Multidimensional-Array Basics
  • example declaration
  • int table new int 106
  • or
  • int table
  • table new int106
  • SomeClass entry new SomeClass10080

45
  • class InterestTable

P P0(1r)
46
  • multidimentional array as a parameter

47
Multidimensional Arrays
  • Multidimensional arrays are implemented in Java
    using one-dimensional arrays.
  • Consider
  • int table new int106
  • The array table is a one-dimensional array of
    length 10.
  • Its base type is int.
  • Therefore, it is an array of arrays.

48
Multidimensional Arrays, cont.
  • This permits us to use the length instance
    variable, instead of an integer literal, to
    control a for loop used to initialize or print
    the values of the elements of an array.
  • example
  • for (r 0 r lt table.length r)
  • for (c 0 c lt tabler.length c)

49
(No Transcript)
50
Ragged Arrays
  • Since a two-dimensional array in Java is an array
    of arrays, each row can have a different number
    of elements (columns).
  • Arrays in which rows have different numbers of
    elements are called ragged arrays.
  • Example
  • int b new int3
  • b0 new int5
  • b1 new int7
  • b2 new int4
Write a Comment
User Comments (0)
About PowerShow.com