Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.

Similar presentations


Presentation on theme: "Introduction to Arrays in Java Corresponds with Chapter 6 of textbook."— Presentation transcript:

1 Introduction to Arrays in Java Corresponds with Chapter 6 of textbook

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

3 Introducing Arrays Array is a data structure that represents a collection of the same types of data.

4 Declaring Arrays datatype[] arrayname; datatype[] arrayname;Example: int[] myList; datatype arrayname[]; datatype arrayname[];Example: int myList[]; 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. NOTE: declaring the array does not create it! No memory is allocated for individual array elements. This requires a separate creation step.

5 Creating Arrays arrayName = new datatype[arraySize]; 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. Example: myList = new double[10]; Java reserved word for object creation Integer value indicates the number of elements

6 Declaring and Creating in One Step datatype[] arrayname = new datatype[] arrayname = new datatype[arraySize]; datatype[arraySize]; double[] myList = new double[10]; double[] myList = new double[10]; datatype arrayname[] = new datatype[arraySize]; 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).

7 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

8 Initializing Arrays Using a loop: Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = someValue; Declaring, creating, initializing in one step: 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).

9 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;

10 Listing 6.1 – TestArray Class 1)Declaring the reference variable 2)Creating the array 3)Assigning the array’s memory location to the reference variable 4)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).

11 How Variables are Kept in Memory Two categories of variables Two categories of variables Value – contains actual data (integer, float, double, boolean, or other primitive data type) 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). 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) Two different locations of memory (more to come later) Frame Stack – contains local variables and parameters of methods Frame Stack – contains local variables and parameters of methods Heap – contains objects and arrays Heap – contains objects and arrays The new keyword always creates an object or array and places it in the heap. The new keyword always creates an object or array and places it in the heap.

12 Heap Frame Stack main’s frame args 1)Declaring the reference variable numbers 2) Creating the array on the heap 012345 3) Assigning the array’s memory location to the reference variable

13 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

14 Assume the user enters the numbers: 3, 2, 7, 5, 7, 1 Heap Frame Stack main’s frame args numbers 012345 i 0 32 1 7 2 5 3 7 4 1 5 6 Looping through the array Note the use of the loop counter to index into the array length is a property of array objects, indicating the number of elements

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

16 Heap Frame Stack main’s frame args numbers 012345 i 1 Looping through the array 3 27571 3 max 3 2 7 3456

17 Listing 6.1 – TestArray Class This use of a loop with an array is similar to a linear search. More on search routines later.

18 Listing 6.1 – TestArray Class Note the concatenation of array elements to a string, again, repetitively in a loop.

19 Parallel Arrays Parallel arrays should have same length Same index position  same entity

20 Program output:

21 M ultidimensional Array s 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. Element [0][0]Element [0][1] Element [1][0] Element [3][1] Element [2][1] Element [1][1] Element [3][0] Element [2][0]

22 Declaring Multidimensional Arrays datatype[][] arrayname; datatype[][] arrayname;Example: int[][] myList; datatype arrayname[][]; datatype arrayname[][];Example: int myList[][]; 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. NOTE: declaring the array does not create it! No memory is allocated for individual array elements. This requires a separate creation step.

23 Creating Multidimensional Arrays arrayName = new datatype[rowSize][columnSize]; 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. Example: myList = new double[10][4]; Java reserved word for object creation Integer value indicates the number of columns Integer value indicates the number of rows

24 Declaring and Creating in One Step datatype[][] arrayname = new datatype[][] arrayname = new datatype[rowSize][columnsize]; datatype[rowSize][columnsize]; double[][] myList = new double[10][3]; double[][] myList = new double[10][3]; datatype arrayname[][] = new datatype[rowSize][columnSize]; 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).

25 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}, {1.9, 2.9}, {3.4, 3.5} {3.4, 3.5}}; NOTE: when using an array, the values inside the square brackets [] [] indicate the indexes that you are looking at (i.e. the specific element). A 2-D array is an array of arrays Use a nested loop to access all elements of a 2-D array, loop counters represent row and column indexes to the array.

26 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

27 Ragged Arrays When rows in a 2-d array can vary in size Int [] [] myList = { {1, 2, 3}, {1, 2}, {1}};

28 Listing 6.12 – TotalScore Class 1)Declaring the reference variable 2)Creating the 3-d array d1 = student (7) d2 = exam (5) d3 = exam part (2) 3)Assigning the array’s memory location to the reference variable

29 How Variables are Kept in Memory Two categories of variables Two categories of variables Value – contains actual data (integer, float, double, boolean, or other primitive data type) 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). 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) Two different locations of memory (more to come later) Frame Stack – contains local variables and parameters of methods Frame Stack – contains local variables and parameters of methods Heap – contains objects and arrays Heap – contains objects and arrays The new keyword always creates an object or array and places it in the heap. The new keyword always creates an object or array and places it in the heap.

30 Heap Frame Stack main’s frame args 1)Declaring the reference variable scores 3) Assigning the array’s memory location to the reference variable 20.522.5127.5… [0,0,0] [0,0,1][0,1,0] [0,1,1] … Note: 7 rows, 5 columns with 2 parts each for a total of 70 elements – only 4 are shown

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

32

33 Program output:

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


Download ppt "Introduction to Arrays in Java Corresponds with Chapter 6 of textbook."

Similar presentations


Ads by Google