tailieunhanh - Lecture Introduction to Java programming - Chapter 7: Multidimensional arrays
The preceding chapter introduced how to use one-dimensional arrays to store linear collections of elements. You can use a two-dimensional array to store a matrix or a table. For example, the following table that describes the distances between the cities can be stored using a two-dimensional array. | Chapter 7 Multidimensional Arrays Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a matrix or a table. For example, the following table that describes the distances between the cities can be represented using a two-dimensional array. Objectives To give examples of representing data using two-dimensional arrays (§). To declare variables for two-dimensional arrays, create arrays, and access array elements in a two-dimensional array using row and column indexes (§). To program common operations for two-dimensional arrays (displaying arrays, summing all elements, finding min and max elements, and random shuffling) (§). To pass two-dimensional arrays to methods (§). To write a program for grading multiple-choice questions using two-dimensional arrays (§). To solve the closest-pair problem using two-dimensional arrays (§). To check a Sudoku solution using two-dimensional arrays (§). To use multidimensional arrays (§). Declare/Create Two-dimensional Arrays // Declare array ref var dataType[][] refVar; // Create array and assign its reference to variable refVar = new dataType[10][10]; // Combine declaration and creation in one statement dataType[][] refVar = new dataType[10][10]; // Alternative syntax dataType refVar[][] = new dataType[10][10]; Declaring Variables of Two-dimensional Arrays and Creating Two-dimensional Arrays int[][] matrix = new int[10][10]; or int matrix[][] = new int[10][10]; matrix[0][0] = 3; for (int i = 0; i Two-dimensional Array Illustration 4 array[0].length? 3 5 matrix[0].length? 5 Declaring, Creating, and Initializing Using Shorthand Notations You can also use an array initializer to declare, create and initialize a two-dimensional array. For example, | Chapter 7 Multidimensional Arrays Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a matrix or a table. For example, the following table that describes the distances between the cities can be represented using a two-dimensional array. Objectives To give examples of representing data using two-dimensional arrays (§). To declare variables for two-dimensional arrays, create arrays, and access array elements in a two-dimensional array using row and column indexes (§). To program common operations for two-dimensional arrays (displaying arrays, summing all elements, finding min and max elements, and random shuffling) (§). To pass two-dimensional arrays to methods (§). To write a program for grading multiple-choice questions using two-dimensional arrays (§). To solve the closest-pair problem using two-dimensional arrays (§). To check a Sudoku solution using .
đang nạp các trang xem trước