340 likes | 639 Vues
Introduction to Arrays in Java. Corresponds with Chapter 6 of textbook. What is an Array?. Array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects . This means array variables are references , not value variable.
E N D
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook
What is an Array? Array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. This means array variables are references, not value variable. Individual data items in arrays are called elements. Elements are stored consecutively (contiguously) in memory. Each element of an array is indexed.Indexing begins at 0.
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Declaring Arrays Either syntax will work. The [] indicates that the variable being declared will be a reference to an array. Each element of the array will be a data item of the specified data type. • datatype[] arrayname; Example: int[] myList; • datatype arrayname[]; Example: int myList[]; NOTE: declaring the array does not create it! No memory is allocated for individual array elements. This requires a separate creation step.
Java reserved word for object creation Integer value indicates the number of elements Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10]; NOTE: the new keyword creates an object or array. The object or array is created in a location of memory called the heap. A reference (pointer) to the array is assigned (via =) to the variable.
Declaring and Creatingin One Step • datatype[] arrayname = new datatype[arraySize]; double[] myList = new double[10]; • datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10]; NOTE: when creating an array, the value inside the square brackets [ ] indicates the size you want the array to be (i.e. the number of elements).
The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length returns 10
Initializing Arrays • Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = someValue; • Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; NOTE: when using an array, the value inside the square brackets [ ] indicates the index that you are looking at (i.e. the specific element).
Declaring, creating, initializing Using the Shorthand Notation double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
Listing 6.1 – TestArray Class • Declaring the reference variable • Creating the array • Assigning the array’s memory location to the reference variable • NOTE: TOTAL_NUMBERS is a constant with value 6 Note: new is an operator that creates an object (an array is an example of an object).
How Variables are Kept in Memory • Two categories of variables • Value – contains actual data (integer, float, double, boolean, or other primitive data type) • Reference – contains a reference (pointer) to an object or array that is located on the heap (as a result of using the new operator). • Two different locations of memory (more to come later) • FrameStack – contains local variables and parameters of methods • Heap – contains objects and arrays • The new keyword always creates an object or array and places it in the heap.
3) Assigning the array’s memory location to the reference variable • Declaring the reference variable 2) Creating the array on the heap main’s frame args 0 1 2 3 4 5 numbers Frame Stack Heap
Listing 6.1 – TestArray Class In a loop, read user input and insert into the array. Note use of length property to decide when to end loop
length is a property of array objects, indicating the number of elements Note the use of the loop counter to index into the array main’s frame 3 2 7 5 7 1 args i 0 1 2 3 4 5 numbers 5 4 1 2 0 3 Looping through the array Assume the user enters the numbers: 3, 2, 7, 5, 7, 1 6 Frame Stack Heap
Listing 6.1 – TestArray Class This loop is a useful technique for finding the largest value in an array. Note the if statement nested in the for loop. Note the use of the loop counter to index into the array.
main’s frame max i args 0 1 2 3 4 5 numbers Looping through the array 2 7 5 7 1 3 4 3 2 6 5 1 3 3 7 Frame Stack Heap
This use of a loop with an array is similar to a linear search. More on search routines later. Listing 6.1 – TestArray Class
Note the concatenation of array elements to a string, again, repetitively in a loop. Listing 6.1 – TestArray Class
Parallel Arrays Parallel arrays should have same length Same index position same entity
Element [0][0] Element [0][1] Element [1][0] Element [1][1] Element [2][0] Element [2][1] Element [3][0] Element [3][1] Multidimensional Arrays A multidimensional array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. This means array variables are references, not value variable. Individual data items in arrays are called elements. Elements are stored consecutively (contiguously) in memory. Can vary in size from two to n.
Declaring Multidimensional Arrays Either syntax will work. The [][] indicates that the variable being declared will be a reference to an array. Each element of the array will be a data item of the specified data type. • datatype[][] arrayname; Example: int[][] myList; • datatype arrayname[][]; Example: int myList[][]; NOTE: declaring the array does not create it! No memory is allocated for individual array elements. This requires a separate creation step.
Java reserved word for object creation Integer value indicates the number of columns Integer value indicates the number of rows Creating Multidimensional Arrays arrayName = new datatype[rowSize][columnSize]; Example: myList = new double[10][4]; NOTE: the new keyword creates an object or array. The object or array is created in a location of memory called the heap. A reference (pointer) to the array is assigned (via =) to the variable.
Declaring and Creatingin One Step • datatype[][] arrayname = new datatype[rowSize][columnsize]; double[][] myList = new double[10][3]; • datatype arrayname[][] = new datatype[rowSize][columnSize]; double myList[][] = new double[10][3]; NOTE: when creating a multidimensional array, the value inside the square brackets [ ] [ ] indicates the number of rows and columns. The total size of the array is the rows multiplied by the columns (10 * 3 = 30 elements for the example above).
A 2-D array is an array of arrays Initializing 2-Dimensional Arrays for (int r =0; r<row; r++) { for (int c = 0; c < column; c++){ myList[r][c]=someValue; } } double[][] myList = { {1.9, 2.9}, {3.4, 3.5} }; Use a nested loop to access all elements of a 2-D array, loop counters represent row and column indexes to the array. NOTE: when using an array, the values inside the square brackets [] [] indicate the indexes that you are looking at (i.e. the specific element).
Obtaining Lengths of Multidimensional Arrays for (int r=0; r<myList.length; r++){ for (int c = 0; c<myList[r].length; c++) { print(myList[r][c]); } } NOTE: The length determines the number of elements myList.length determines the number of rows myList[r].length determines the number of elements for a given row
Ragged Arrays When rows in a 2-d array can vary in size Int [] [] myList = { {1, 2, 3}, {1, 2}, {1} };
Listing 6.12 – TotalScore Class • Declaring the reference variable • Creating the 3-d array • d1 = student (7) • d2 = exam (5) • d3 = exam part (2) • Assigning the array’s memory location to the reference variable
How Variables are Kept in Memory • Two categories of variables • Value – contains actual data (integer, float, double, boolean, or other primitive data type) • Reference – contains a reference (pointer) to an object or array that is located on the heap (as a result of using the new operator). • Two different locations of memory (more to come later) • FrameStack – contains local variables and parameters of methods • Heap – contains objects and arrays • The new keyword always creates an object or array and places it in the heap.
3) Assigning the array’s memory location to the reference variable • Declaring the reference variable main’s frame args scores Note: 7 rows, 5 columns with 2 parts each for a total of 70 elements – only 4 are shown [0,0,0] [0,0,1] [0,1,0] [0,1,1] 7.5 20.5 12 22.5 … … Frame Stack Heap
Listing 6.12 – TotalScore Class In a loop, total all of the exam parts together. Display the total for each student. Note: 3-D array loop nested 3-deep.
Contents of memory at breakpoint Note: a 2-dimensional array is implemented as an array of arrays. Each element of the first dimension is a reference to a single dimensional array.