Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays.

Similar presentations


Presentation on theme: "Arrays."— Presentation transcript:

1 Arrays

2 Java Collections Uncovered
Collection classes (particularly ArrayList) are implemented using arrays Arrays are structures that are part of the Java language If you need a collection of primitive types (e.g., ints or doubles), arrays are arguably more convenient to use than ArrayLists Wrapper classes can be avoided Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 2

3 Arrays Definition In Java,
pronounced “ar-ray”, w/ stress on 2nd syllable Definition sequence of elements of the same type each element is accessed through an index In Java, declaration: double[] nums; creation: nums = new double[8]; use: nums[3] = 6.6; Note: starting index is 0 (0 to 7, above) double nums[] is also legal, but double[] nums is preferred, since it emphasizes that the type is double[] (“double array” or “array of doubles”) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 3

4 Visualizing an Array nums null Declare: double[] nums;
Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 4

5 Visualizing an Array nums null Declare: double[] nums;
0.0 1 2 3 4 5 6 7 null Declare: double[] nums; Create: nums = new double[8]; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 5

6 Visualizing an Array nums 6.6 Declare: double[] nums;
0.0 1 2 3 4 5 6 7 6.6 Declare: double[] nums; Create: nums = new double[8]; Use: nums[3] = 6.6; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 6

7 Array of Objects Declaration Creation of the Array Creation of Objects
Circle[] circles; Creation of the Array circles = new Circle[8]; creates an array of references to Circles but no actual Circles yet Creation of Objects for ( i = 0; i < 8; i++ ) { circles[i] = new Circle( 10 + i * 10, 10, 5 ); } creates the Circles themselves and assigns them to the references Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 7

8 Visualizing an Array of Objects
circles null Declare: Circle[] circles; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 8

9 Visualizing an Array of Objects
1 2 3 4 null Circle-type references circles null Declare: Circle[] circles; Create array: circles = new Circle[5]; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 9

10 Visualizing an Array of Objects
Circle-type references Circle x y radius circles null Circle x y radius null 1 null Circle x y radius 2 null Circle x y radius null 3 Declare: Circle[] circles; 4 Circle x y radius null Create array: circles = new Circle[5]; Create objects: for ( i = 0; i < 5; i++ ) { circles[i] = new Circle( 10 + i * 10, 10, 5 ); } Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 10

11 Visualizing an Array of Objects
Circle-type references Circle x y radius circles null Circle x y radius null 1 null Circle x y radius 2 null null 3 Declare: Circle[] circles; 4 Circle x y radius null Create array: circles = new Circle[5]; Circle x y radius Circle x y radius Create objects: for ( i = 0; i < 5; i++ ) { circles[i] = new Circle( 10 + i * 10, 10, 5 ); } Use objects: e.g., circles[3].getX(); (returns 40) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 11

12 More About Arrays Arrays are objects Special features
the array variable is just a reference to the actual array that contains the values need to use “new” after declaring passed as a reference when used as method parameter Special features a public final int length field returns the array size in recent example, circles.length would return 5 [] operator only work with arrays ArrayIndexOutOfBounds exception valid indices for array of size n: 0 to n-1 any acces to other indices causes an error Array size can’t be changed after array is created To expand array, we need to create a new array, copy old array contents, then point array variable to new array Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 12

13 Arrays versus ArrayList
Maximum size For arrays, need to establish establish this quantity upon creation For ArrayLists, underlying array grows and shrinks as needed Types and casting Arrays are “typed”; i.e., programmer indicates what kind of elements are allowed in the collection ArrayLists take any Object. Need to cast upon invoking the get() method. Need to use a wrapper class when storing primitives Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 13

14 Multi-dimensional Arrays
A natural extension of simple (1D) arrays 2D declaration: char[][] grid; think “array of arrays” Array creation grid = new char[10][20]; // 10 rows, 20 columns Another way grid = new char[10][]; // creates array of 10 char[]’s for (i = 0; i < 10; i++) { grid[i] = new char[20]; // creates a size-20 array } This way allows for varying row sizes Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 14

15 Visualizing 2D Arrays char[][] null C Declare: char[][] grid;
1 2 3 4 char[]-type references char[][] null C Declare: char[][] grid; Create array of rows: grid = new char[5][]; Create rows: for ( i = 0; i < 5; i++ ) { grid[i] = new char[3]; } Use objects: e.g., grid[3][2] = ‘C’ Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 15

16 Using 2D Arrays To refer to individual element, use two indices
e.g., grid[5][18] = ‘X’; Using only one index refers to a single dimensional array e.g., grid[5] refers to row 5 grid[5].length is the length of row 5 (in this case, it’s 20) The array variable by itself refers to the top-level array (i.e., the array of rows) grid.length is the length of the array of rows (i.e., it’s the number of rows) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 16

17 Sorting Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 17

18 Sorting Goal: reorder array elements to be in a desired order
Comparing Strings s1.compareTo( s2 ) – returns an int: 0 if s1 and s2 have same content > 0 if s1 > s2 (e.g., “Homer” > “Bart”) < 0 if s1 < s2 (e.g., “Lisa” < “Maggie” ) Note: Uses Unicode values, so all Capital letters are considered less than lowercase letters (e.g., “G” < “a”) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 18

19 Bubble Sort Starting from beginning, swap adjacent pairs, putting greater value to right At the end of one pass, greatest value will “bubble” to rightmost position Repeat for next-to-greatest value, etc. Q W E R T Q W E R T Q E W R T Q E R W T Q E R T W Greatest Value is now at rightmost position Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 19

20 Bubble Sort end-1 end 1 2 3 4 Q W E R T First loop goes from i=0 to 3,
1 2 3 4 public void sort( String[] a ) { for ( int end = a.length-1; end > 0; end--) for ( int i = 0; i < end; i++ ) if ( a[i].compareTo( a[i+1] ) > 0 ) { // swap String temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } Q W E R T First loop goes from i=0 to 3, repeat for i=0 to 2, i=0 to 1, i=0 Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 20

21 Sorting Objects assuming GradeRecord has
public void sort( GradeRecord[] a ) { for ( int end = a.length-1; end > 0; end--) for (int i = 0; i < end; i++) if ( a[i].getName().compareTo( a[i+1].getName() ) > 0 ) { // swap the whole object (not just the Strings) GradeRecord temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } assuming GradeRecord has a getName() method that returns a String Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 21

22 Performance of Sorting
How long does BubbleSort take? N-1 turns + N-2 + N-3, etc. Roughly N2 for N items We say O(N2) time (“order N2” time) Not very efficient for large N There are faster sort algorithms Fastest comparison-based sort is O(N log N) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 22


Download ppt "Arrays."

Similar presentations


Ads by Google