Chapter 5 Arrays Introducing Arrays Declaring Array Variables
Chapter 5 Arrays �Introducing Arrays �Declaring Array Variables, Creating Arrays, and Initializing Arrays �Copying Arrays �Multidimensional Arrays �Search and Sorting Methods
Introducing Arrays Array is a data structure that represents a collection of the same types of data. An Array of 10 Elements of type double
Declaring Array Variables Example: double[] my. Array; Example: double my. Array[];
Creating Arrays Example: my. Array = new double[10]; my. Array[0] references the first element in the array. my. Array[9] references the last element in the array.
Declaring and Creating in One Step double[] my. Array = new double[10];
The Length of Arrays �Once an array is created, its size is fixed. It cannot be changed. You can find its size using array. Variable. length
Initializing Arrays �Using a loop: for (int i = 0; i < my. List. length; i++) my. Array[i] = i; �Declaring, creating, initializing in one step: double[] my. Array = {1. 9, 2. 9, 3. 4, 3. 5}; This shorthand syntax must be in one statement.
Declaring, creating, initializing Using the Shorthand Notation double[] my. Array = {1. 9, 2. 9, 3. 4, 3. 5}; This shorthand notation is equivalent to the following statements: double[] my. Array = new double[4]; my. Array[0] = 1. 9; my. Array[1] = 2. 9; my. Array[2] = 3. 4; my. Array[3] = 3. 5;
Copying Arrays Using a loop: int[] source. Array = {2, 3, 1, 5, 10}; int[] target. Array = new int[source. Array. length]; for (int i = 0; i < source. Arrays. length; i++) target. Array[i] = source. Array[i];
9. 4 Multidimensional Arrays �Arrays studied so far have one dimension … length �It is also possible to define arrays with more than one dimension �Two dimensional arrays can store values arranged in rows and columns �Three dimensional arrays can store values arranged in rows, columns, and ranks (or pages)
Multidimensional Arrays Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays int[][] matrix = new int[10]; or int matrix[][] = new int[10]; matrix[0][0] = 3; for (int i=0; i<matrix. length; i++) for (int j=0; j<matrix[i]. length; j++) { matrix[i][j] = (int)(Math. random()*1000); } double[][] x;
Multidimensional Array Illustration
Declaring, Creating, and Initializing Using Shorthand Notations You can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example, int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; This is equivalent to the following statements: int[][] array[0][0] = array[1][0] = array[2][0] = array[3][0] = = new int[4][3]; 1; array[0][1] = 2; array[0][2] = 4; array[1][1] = 5; array[1][2] = 7; array[2][1] = 8; array[2][2] = 10; array[3][1] = 11; array[3][2] 3; 6; 9; = 12;
Lengths of Multidimensional Arrays int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; array. length array[0]. length array[1]. length array[2]. length
Ragged Arrays Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, {3, 4, 5}, {5} };
Exampe �Declare a two dimenstional arrays with 5 row and 5 where each row hold 10 integers.
Example what does this program do � � � � class Array. Example 2 { public static void main (String[] args) { int[][] table = new int[3][4]; int row, col; } } for (row = 0; row < table. length; row++) { for (col = 0; col < table[row]. length; col++) { table[row][col] = row + col; }
What what is the output of this program � � � � � � class Array. Example { public static void main (String[] args) { int[ ][ ] table = new int[3][4]; int row, col; for (row = 0; row < 3; row++) { for (col = 0; col < 4; col++) { table[row][col] = row + col; } } for (int row = 0; row < 3; row++) { for (int col = 0; col < 4; col++) { System. out. print(table[row][col]); } System. out. println(); } }
summary �Multi-dimensional arrays �Length of a multidimensional array �Manupilating multidimensional array.
- Slides: 19