0% found this document useful (0 votes)
31 views49 pages

Java™ How To Program, 9/e: Reserved

Java file

Uploaded by

akshay choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views49 pages

Java™ How To Program, 9/e: Reserved

Java file

Uploaded by

akshay choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49

Java™ How to Program, 9/e

© Copyright 1992-2012 by Pearson Education, Inc. All Rights


Reserved.
 Two-dimensional arrays are often used to represent tables
of values consisting of information arranged in rows and
columns.
 Identify a particular table element with two indices.
 By convention, the first identifies the element’s row and the second
its column.
 Multidimensional arrays can have more than two
dimensions.
 Java does not support multidimensional arrays directly
 Allows you to specify one-dimensional arrays whose elements are
also one-dimensional arrays, thus achieving the same effect.
 In general, an array with m rows and n columns is called an
m-by-n array.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 Multidimensional arrays can be initialized with array
initializers in declarations.
 A two-dimensional array b with two rows and two
columns could be declared and initialized with nested
array initializers as follows:
int[][] b = { { 1, 2 }, { 3, 4 } };
 The initial values are grouped by row in braces.
 The number of nested array initializers (represented by sets of
braces within the outer braces) determines the number of rows.
 The number of initializer values in the nested array initializer
for a row determines the number of columns in that row.
 Rows can have different lengths.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 The lengths of the rows in a two-dimensional array are
not required to be the same:
int[][] b = { { 1, 2 }, { 3, 4, 5 } };
 Each element of b is a reference to a one-dimensional array of
int variables.
 The int array for row 0 is a one-dimensional array with two
elements (1 and 2).
 The int array for row 1 is a one-dimensional array with three
elements (3, 4 and 5).

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 A multidimensional array with the same number of columns in
every row can be created with an array-creation expression.
int[][] b = new int[ 3 ][ 4 ];
 3 rows and 4 columns.
 The elements of a multidimensional array are initialized when
the array object is created.
 A multidimensional array in which each row has a different
number of columns can be created as follows:
int[][] b = new int[ 2 ][ ]; // create 2 rows
b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0
b[ 1 ] = new int[ 3 ]; // create 3 columns for row 1
 Creates a two-dimensional array with two rows.
 Row 0 has five columns, and row 1 has three columns.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Figure 7.17 demonstrates initializing two-dimensional
arrays with array initializers and using nested for
loops to traverse the arrays.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 In most semesters, students take several exams.
 Figure 7.18 contains a version of class GradeBook
that uses a two-dimensional array grades to store the
grades of a number of students on multiple exams.
 Each row represents a student’s grades for the entire course.
 Each column represents the grades of all the students who took
a particular exam.
 In this example, we use a ten-by-three array containing
ten students’ grades on three exams.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Variable-length argument lists
 Can be used to create methods that receive an unspecified
number of arguments.
 Parameter type followed by an ellipsis (...) indicates that the
method receives a variable num-ber of arguments of that
particular type.
 The ellipsis can occur only once at the end of a parameter list.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 Command-line arguments
 Can pass arguments from the command line to an application.
 Arguments that appear after the class name in the java
command are received by main in the String array args.
 The number of command-line arguments is obtained by
accessing the array’s length attribute.
 Command-line arguments are separated by white space, not
commas.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Arrays class
 Provides static methods for common array manipulations.
 Methods include
 sort for sorting an array (ascending order by default)
 binarySearch for searching a sorted array
 equals for comparing arrays
 fill for placing values into an array.
 Methods are overloaded for primitive-type arrays and
for arrays of objects.
 System class static arraycopy method
 Copies contents of one array into another.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 Java API provides several predefined data structures, called
collections, used to store groups of related objects.
 Each provides efficient methods that organize, store and retrieve your
data without requiring knowledge of how the data is being stored.
 Reduce application-development time.
 Arrays do not automatically change their size at execution time
to accommodate additional elements.
 ArrayList<T> (package java.util) can dynamically
change its size to accommodate more elements.
 T is a placeholder for the type of element stored in the collection.
 This is similar to specifying the type when declaring an array, except that
only nonprimitive types can be used with these collection classes.
 Classes with this kind of placeholder that can be used with any
type are called generic classes.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Figure 7.24 demonstrates some common ArrayList
capabilities.
 An ArrayList’s capacity indicates how many items
it can hold without growing.
 When the ArrayList grows, it must create a larger
internal array and copy each element to the new array.
 This is a time-consuming operation. It would be inefficient for
the ArrayList to grow each time an element is added.
 An ArrayList grows only when an element is added and the
number of elements is equal to the capacity—i.e., there is no
space for the new element.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Method add adds elements to the ArrayList.
 One-argument version appends its argument to the end of the
ArrayList.
 Two-argument version inserts a new element at the specified
position.
 Collection indices start at zero.
 Method size returns the number of elements in the
ArrayList.
 Method get obtains the element at a specified index.
 Method remove deletes an element with a specific value.
 An overloaded version of the method removes the element at the
specified index.
 Method contains determines if an item is in the
ArrayList.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Drawing arcs in Java is similar to drawing ovals—an arc is
simply a section of an oval.
 Graphics method fillArc draws a filled arc.
 Method fillArc requires six parameters.
 The first four represent the bounding rectangle in which the arc will be
drawn.
 The fifth parameter is the starting angle on the oval, and the sixth
specifies the sweep, or the amount of arc to cover.
 Starting angle and sweep are measured in degrees, with zero degrees
pointing right.
 A positive sweep draws the arc counterclockwise.
 Method drawArc requires the same parameters as fillArc,
but draws the edge of the arc rather than filling it.
 Method setBackground changes the background color of a
GUI component.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.

You might also like