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.
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).
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.
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.
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.
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.
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.
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.
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.
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.