Arrays F Introducing Arrays F Declaring Array Variables
Arrays F Introducing Arrays F Declaring Array Variables F Creating Arrays, and Initializing Arrays F Copying Arrays F Multidimensional Arrays
Introducing Arrays Array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. An Array of 10 Elements of type double
Declaring Array Variables F datatype[] arrayname; Example: int[] my. List; F datatype arrayname[]; Example: int my. List[];
Creating Arrays array. Name = new datatype[array. Size]; Example: my. List = new double[10];
Declaring and Creating in One Step F datatype[] arrayname = new datatype[array. Size]; double[] my. List = new double[10]; F datatype arrayname[] = new datatype[array. Size]; double my. List[] = new double[10];
Initializing Arrays F Using a loop: for (int i = 0; i < my. List. length; i++) my. List[i] = (double)i; F Declaring, creating, initializing in one step: double[] my. List = {1. 9, 2. 9, 3. 4, 3. 5};
Passing Arrays to Methods Java uses pass by value to pass parameters to a method. There are important differences between passing a value of variables of primitive data types and passing arrays. F For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method. F For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument. F
Copying Arrays?
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];
The arraycopy Utility arraycopy(source. Array, src_pos, target. Array, tar_pos, length); Example: System. arraycopy(A 1, 0, A 2, 0, A 1. length);
Multidimensional Arrays F int[][] matrix = new int[10]; or int matrix[][] = new int[10]; F int[][][] matrix = new int[10][20][30]; for (int i=0; i<matrix. length; i++) for (int j=0; j<matrix[i]. length; j++) { matrix[i][j] = (int)(Math. random()*1000); }
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} };
Array of arrays int two. D[][] = new int[4][]; two. D[0] = new int[4]; two. D[1] = new int[3]; two. D[2] = new int[2]; two. D[3] = new int[1];
Array of Objects Circle[] circle. Array = new Circle[10]; An array of objects is actually an array of reference variables. F So invoking circle. Array[1]. find. Area() involves two levels of referencing as shown in the next figure. circle. Array references to the entire array. circle. Array[1] references to a Circle object. F
Array of Objects, cont. • Invoking circle. Array[1]. find. Area() involves two levels of referencing as shown in the next figure. . circle. Array references to the entire array. circle. Array[1] references to a Circle object
Summarizing the areas of the circles // Declaration and creation Circle[] circle. Array = new Circle[10]; for(int k=0; k < circle. Array. length; k++) { circle. Array[k] = new Circle(Math. random*100); } // Print radius and calculate the total area double total. Area = 0; for(k=0; k < circle. Array. length; k++) { System. out. println(“k=“+k+”) R=“+circle. Array[k]. get. Radius()); total. Area += circle. Array[k]. find. Area(); } System. out. println(“Area=”+ total. Area);
- Slides: 16