Title: Arrays
1Arrays
2The Very Large Array
3The 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.
4The Very Large Array
5Introduction 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.
6Introduction 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.
7Creating 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).
8Creating and Accessing Arrays
9(No Transcript)
10Array 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.
11The 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.
12Indices 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.
13Initializing 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
14Arrays 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.
15Case 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
16Case Study Using an Array as an Instance
Variable, cont.
- tasks
- obtain the data (getFigures)
- update the instance variables (update)
- display the results (displayResults)
17Pseudocode
- 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.
18Pseudocode - 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)
23Indexed Variables as Method Arguments
24Arrays 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.
25Arguments 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
26Use 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.
27Making 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
28Determining 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 30 31Programming 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.
32Programming 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 34More 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.
37Partially Filled Arrays
38Sorting 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.
39Selection Sort
40 41Sorting Algorithms
- Heap Sort
- Bubble Sort
- Quick Sort
- .
42Bubble Sort
Programming Project 4
43Multidimensional Arrays
44Multidimensional-Array Basics
- example declaration
- int table new int 106
- or
- int table
- table new int106
- SomeClass entry new SomeClass10080
45P P0(1r)
46- multidimentional array as a parameter
47Multidimensional 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.
48Multidimensional 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