oop-java-unit-iv-batu-prp-
oop-java-unit-iv-batu-prp-
By:Prof.Pankaj R. Patil
Introduction
• Arrays
– Data structures
– Related data items of same type
– Remain same size once created
• Static entries
• Array
– Group of contiguous memory locations
• Each memory location has same name
• Each memory location has same type
- The elements of an array can be either primitive types or reference
types
- we specify the name of the reference to the array and the position
number of the element in the array.
- The position number of the element is called the element’s index or
subscript.
2002 Prentice Hall. All rights reserved.
Name of array (Note c[ 0 ] -45
that all elements of c[ 1 ] 6
this array have the
same name, c) c[ 2 ] 0
c[ 3 ] 72
c[ 4 ] 1543
c[ 5 ] -89
c[ 6 ] 0
c[ 7 ] 62
c[ 8 ] -3
• Subscript
– Also called an index
– Position number in square brackets
– Must be integer or integer expression
a = 5;
b = 6;
c[ a + b ] += 2;
• Adds 2 to c[ 11 ]
• Subscripted array name is an lvalue
2002 Prentice Hall. All rights reserved.
Arrays (cont.)
• Examine array c
– c is the array name
– c.length accesses array c’s length
– c has 12 elements ( c[0], c[1], … c[11] )
• The value of c[0] is –45
• The brackets ([]) are in highest level of precedence in
Java
• Declaring arrays
-When an array is declared, the type of the array and the square brackets can be combined at the
beginning of the declaration to indicate that all the identifiers in the declaration are array variables.
For example, the declaration
double[] array1, array2;
- indicates that array1 and array2 are each “array of double” variables. The preceding declaration is
equivalent to:
double array1[];
double array2[];
or
double[] array1;
double[] array2;
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
Enhanced for Statement
• iterates through the elements of an array or a collection without using a counter (thus
avoiding the possibility of “stepping outside” the array).
• The syntax of an enhanced for statement is:
for ( parameter : arrayName )
statement
• where parameter has two parts—a type and an identifier (e.g., int number)—and
arrayName is the array through which to iterate.
• Figure 7.12 uses the enhanced for statement (lines 12–13) to sum the integers in an array of
student grades. The type specified in the parameter to the enhanced for is int, because
array contains int values—the loop selects one int value from the array during each
iteration. The enhanced for statement iterates through successive values in the array one by
one. The enhanced for header can be read as “for each iteration, assign the next element
of array to int variable number, then execute the following statement.”
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
• Lines 12–13 are equivalent to the following counter-controlled
repetition used in lines 12–13 of Fig. 7.5 to total
the integers in array:
for ( int counter = 0; counter < array.length; counter++ )
total += array[ counter ];
• enhanced for statement can be used only to obtain array elements—
it cannot be used to modify elements. If your program needs to
modify elements, use the traditional counter-controlled for
statement.
By:Prof.Pankaj R. Patil
Passing Arrays to Methods
Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Array name
Fig. 7.14 A double-subscripted array with three rows and four columns.
By:Prof.Pankaj R. Patil
Case Study: Class GradeBook Using a Two- Dimensional Array
Storing Student Grades in a Two-Dimensional Array in Class GradeBook
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
Variable-Length Argument Lists
• With variable-length argument lists, you can create methods that receive an
unspecified number of arguments.
• An argument type followed by an ellipsis (...) in a method’s parameter list
indicates that the method receives a variable number of arguments of that
particular type.
• This use of the ellipsis can occur only once in a parameter list, and the ellipsis,
together with its type, must be placed at the end of the parameter list.
• Figure 7.20 demonstrates method average (lines 7–16), which receives a
variable length sequence of doubles. Java treats the variable-length argument
list as an array whose elements are all of the same type. Hence, the method
body can manipulate the parameter numbers as an array of doubles.
• Method average has a variable-length argument list (line 7), so it can average
as many double arguments as the caller passes. The output shows that each
call to method average returns theBy:Prof.Pankaj
correct value.
R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
Using Command-Line Arguments
• On many systems it is possible to pass arguments from the command line (these are known
as command-line arguments) to an application by including a parameter of type String[]
(i.e., an array of Strings) in the parameter list of main, exactly as we have done in every
application in the book.
• By convention, this parameter is named args.
• When an application is executed using the java command, Java passes the command-line
arguments that appear after the class name in the java command to the application’s main
method as Strings in the array args.
• The number of arguments passed in from the command line is obtained by accessing the
array’s length attribute. For example, the command "java MyClass a b" passes two
command-line arguments, a and b, to application MyClass. Note that command-line
arguments are separated by white space, not commas.
• When this command executes, MyClass’s main method receives the two-element array args
(i.e., args.length is 2) in which args[ 0 ] contains the String "a" and args[ 1 ] contains the
String "b". Common uses of command-line arguments include passing options and file
names to applications.
By:Prof.Pankaj R. Patil
• Figure 7.21 uses three command-line arguments to initialize an array.
• When the program executes, if args.length is not 3, the program
prints an error message and terminates (lines 9–12). Otherwise, lines
14–32 initialize and display the array based on the values of the
command-line arguments.
• The command-line arguments become available to main as Strings in
args.
• Line 16 gets args[ 0 ]—a String that specifies the array size—and
converts it to an int value that the program uses to create the array
in line 17.
• The static method parseInt of class Integer converts its String
argument to an int.
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil
By:Prof.Pankaj R. Patil