1 / 24

Announcements

Learn how to declare, access, and manipulate arrays in Java programming. Understand the concept of indexing, allocation/deallocation, and practical use cases with Java examples.

Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements • Assignment P2 is due on Thursday • Assignment P3 is handed out today • Prelim on Monday the 19th. Coming soooooooooon. Lecture 10

  2. Today’s Topics • Review • Arrays • Allocation/Deallocation • Subscripting • Use of arrays to store large collections of data • Several examples Lecture 10

  3. Review of iteration • For loops • for(j = 0; j < NUM; j++) System.out.println(j); • What do you think for(;;) does, for example? • Invariants • break and continue statements Lecture 10

  4. Hypothetical Problem • Input: zero or more grades from 0 to 100 preceded by the number of grades5 90 85 40 89 12 • Task: read grades and compile information about them • print them in reverse order • print them in increasing order • print a histogram • So, need to read in all the values before processing, need as many variables as grades. . . • How to do this using the Java we know so far? Lecture 10

  5. Use arrays instead! g 90 0 • An array is an ordered list of values. • Each value is stored at a position in the array • The number referring to the position is called an index • In Java, array indices begin at 0 1 85 2 40 3 89 4 12 Lecture 10

  6. Deconstruction of this array • g[0] is 90 • g[1] is 85 • g[2] is 40 • g[3] is 89 • g[4] is 12 • g.length is the number of elements in the array, g.length is 5 • In “g[4]”, 4 is the index or subscript Lecture 10

  7. Another example h 1 • h.length is the number of array elements in array h -- here, h.length is 323 0 1 54 2 -32 3 -101 4 82 . . . 322 93 Lecture 10

  8. h i j k Some notation segment number of values in it empty when g[h..i-1] i-h h = i g[i..j] j+1-i j = i-1 g[j+1..k-1] k-(j+1) j = k g Lecture 10

  9. Declaring an array variable null • int[] g; g • float[] averages; averages • Employee[] employees; employees • Declaring a variable does not “allocate” or create the array of elements; it only declares a variable that can contain a reference to an array of elements. • An array is much like a class in that an array variable contains a reference to an array. null null Lecture 10

  10. 0 1 2 3 4 … 204 205 null null null null null ... null null How to get the space? • g = new int[5]; g • Employee[] e = new Employee[206];e 0 1 2 3 4 Lecture 10

  11. More on declaration and accessing • What happens in the statemente[3] = new Employee(“Millett”, 1999); • Given allocated array e, we can reference e[0], e[1] …., but we can also use expressions as array indices: e[2*b], e[i], etc. Lecture 10

  12. Using array elements • Suppose g is an array of integers • Then, g[i] where i is in range can be used just as an integer variable • g[i] = 3; a = g[i]*2; c.setX(g[i]); • System.out.println(“value is ” + g[i]); • Note that arrays are objects -- therefore have the same ‘call by value’ conventions • Java does bounds checking -- throws exception if out of bounds Lecture 10

  13. Example program with arrays // Read in a list of integer grades, preceded by the // number of grades, and print in them in reverse order int n= Integer.parseInt(stdin.readLine()); // number of grades int[ ] g= new int [n]; // g[0..n-1] are the grades // Read in the grades int i= 0; while (i != g.length) { g[i]= Integer.parseInt(stdin.readLine()); i= i+1; } // Print grades in reverse order int k= n; while (k > 0) { System.out.println(g[k - 1]); k = k-1; } } Lecture 10

  14. Rewrite previous with for loops int n= Integer.parseInt(stdin.readLine()); // number of grades int[ ] g= new int [n]; // g[0..n-1] are the grades // Read in the grades for(int i = 0; i < g.length; i++) { g[i]= Integer.parseInt(stdin.readLine());} // Print grades in reverse order for (int k = n; k > 0; k--) { System.out.println(g[k - 1]); } Lecture 10

  15. Histogram example -- explanation Program scheme to print “histogram” of grades // Read in a list of grades in the range 0..100, preceded // by the number of grades, and // print out how many times each grade appears int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will be the no. of times grade i appears // Initialize frequencies f[0..100] to 0. // Read in the grades and make up array f. // Print the grades and their frequencies (print only the grades in 0..10 // that appeared at least once in the input) } Lecture 10

  16. // Read in a list of grades in the range 0..100, preceded by the number // of grades, and print out how many times each grade appears int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will be the no. of times grade i appears // Initialize frequencies f[k] to 0. int k= 0; while (k != f.length) {f[k]= 0; k= k+1;} Lecture 10

  17. // Read in the grades and make up array f int i= 0; // Inv: i grades have been read in and // each f[k], for 0<=k<=100, contains // the no. of times grade k was read in while (i != f.length) { int grade= Integer.parseInt(stdin.readLine() ); f[grade]= f[grade] + 1; i= i+1; } Lecture 10

  18. // Print the grades and their frequency (print only the // grades in 0..100 that appeared at least once in the // input) int i= 0; // Inv: the grades 0..i-1 that occurred at least // once have been printed while (i != f.length) { if (f[i] != 0) System.out.println(“grade: ” + i + “ frequency: ” + f[i]); i= i+1; } Lecture 10

  19. Palindromes • A palindrome is a word that reads the same backwards and forwards. • The empty string • A • AA • ABBA • NOON • The following are palindromes if blanks and punctuation are ignored • able was I ere I saw elba • a man a plan a canal panama Lecture 10

  20. Is a given array a palindrome? // Return the value of the statement “array b is a palindrome” staticpublic bool isPalindrome(char[ ] b) { int i= 0; int j= b.length; // Invariant: b is a palindrome iff b[i..j-1] is. In other // words, b[j..length-1] is the reverse of b[0..i-1] while ( j - i >1) { j= j-1; if (b[i] != b[j]) return false; i= i+1; } // {b[i..j-1] has 0 or 1 elements, so it’s a palindrome} return true; } Lecture 10

  21. Odd Syntax Thing • int[] grades; is equivalent to int grades[]; • Be careful however:int a, b, c;int[] x, y, z;int r[], s, t[]; • Best to associate array brackets with the type not the variable name Lecture 10

  22. Initializer Lists Note curly braces • You can initialize the elements of an array just as you can initialize a variable • Exampleint[] grades = {88, 92, 65, 77, 33}; • grades is now an array of 5 integers with values as shown • Such an initializer list can only be used when the array is first declared Lecture 10

  23. Arrays of objects • Remember: Java allows arrays of more than ints • Suppose you would like an array of strings:String[] words = new String[25]; • We could declare an array of employees, as we saw previously:Employee[] workers = new Employee[25];workers[3] = new Employee(“Millett”, 1999);workers[3].setSalary(50000); Lecture 10

  24. Arrays as parameters • Similar to when passing objects as parameters • Declaration: z(int[] list)Call: z(a) where a is declared int[] z can change elements of the array, but not array reference itself • Can also pass elements of the array as parametersDeclaration: q(int x)Call: q(a[3]) Lecture 10

More Related