Title: More on Arrays
1More on Arrays
- Passing arrays to or from methods
- Arrays of objects
- Command line arguments
- Variable length parameter lists
- Two dimensional arrays
- Reading for this lecture LL 7.3 7.7
2Arrays as Parameters to Methods
- An entire array can be passed as a parameter to a
method - Like any other object, a reference to the array
is passed, making the formal and actual
parameters aliases of each other - Therefore, changing an array element within the
method changes that array element in the original - An individual array element can be passed to a
method as well, in which case the type of the
formal parameter is the same as the element type
3Arrays as Return Values from Methods
- An entire array can be returned by a method as
its return value - Like any other object, a reference to the array
is passed - If the array is declared inside the method as a
local variable, then the returned reference is
the only remaining reference to the array object - An individual array element can be returned by a
method as well, in which case the type of the
returned value is the same as the element type
4Arrays of Objects
- The elements of an array can be object references
- The following declaration reserves space to store
5 references to String objects - String words new String5
- It does NOT create the String objects themselves
- Initially an array of objects holds null
references - Each object stored in an element of an array must
be instantiated separately
5Arrays of Objects
- The words array when initially declared
- A reference to words.length is OK ( 5)
- However, the following reference will throw a
NullPointerException - System.out.println(words0.length())
6Arrays of Objects
- To create some String objects and store them in
elements of the array
words0 new String(friendship) words1
loyalty words2 honor
7Arrays of Objects
- String objects can be created using literals
- The following declaration creates an array object
called verbs with a length of 4 and fills it with
references to four String objects created using
string literals
String verbs "play", "work", "eat", "sleep"
8Arrays of Objects
- To use one of the methods of an object element of
an array - verbs2.equals(eat) // true
- To pass one of the object elements of an array as
a parameter to a method - eat.equals(verbs2) // true
- To return an element of an array
- public String methodName(String verbs)
-
- return verbs2 // eat
9Command-Line Arguments
- Your programs main method is defined as
- public static void main(String args)
- The signature of the main method indicates that
it takes an array of String objects as a
parameter - These values come from command-line arguments
that are provided when the interpreter is invoked - In Dr Java interactions pane, this invocation of
the JVM passes three String objects as arguments
to the main method of StateEval - gt java StateEval pennsylvania texas arizona
10Command Line Arguments
- These strings are stored at indexes 0-2 in the
array args for the main method - The array args will contain
- args0 ? pennsylvania
- args1 ? texas
- args2 ? arizona
- Code in main can print the arguments
- for (String arg args)
- System.out.println(arg)
11Variable Length Parameter Lists
- Suppose we want to create a method to processes a
different amount of data from one invocation to
the next - For example, let's define a method called average
that returns the average of a variable number of
integer parameters
// one call to average three values mean1
average (42, 69, 37) // another call to
average seven values mean2 average (35, 43, 93,
23, 40, 21, 75)
12Variable Length Parameter Lists
- Using special syntax in a formal parameter list,
we can define a method to accept any number of
parameters of the same type - The parameters are automatically put into an
array for easy processing in the method
public double average (int ... list) // see
next slide
13Variable Length Parameter Lists
public double average (int ... list) double
result 0.0 if (list.length ! 0)
int sum 0 for (int num list)
sum num result (double)sum /
list.length return result
14Variable Length Parameter Lists
- A method can accept individual parameters and a
varying number of parameters - The individual parameters must come first in the
formal arguments - The varying number of parameters must come last
in the formal arguments - A single method can accept only one set of
varying parameters
15Two-Dimensional Arrays
- A one-dimensional array stores a list of elements
- A two-dimensional array can be thought of as a
table of elements, with rows and columns
16Two-Dimensional Arrays
- A two-dimensional array is declared specifying
the size of each dimension separately - int scores new int1250
- An array element is referenced using two indexes
- int value scores36
- Each dimension subdivides the previous one into
the specified number of elements - Each dimension has its own length constant
- scores.length 12 // 12 arrays of 50
- scoresanyInteger0Through11.length 50
17Two-Dimensional Arrays
- A two-dimensional array is an array of arrays
- The array stored in one row can be specified
using one index - int scoreRow
- scoreRow scores1 // 50 ints
- scoreRow gets initialized from scores1
- scoreRow.length 50
- // for all n gt 0 n lt 50
- scoreRown scores1n