comp 250 lecture 4
play

COMP 250 Lecture 4 Array lists Sept. 15, 2017 1 Arrays in Java - PowerPoint PPT Presentation

COMP 250 Lecture 4 Array lists Sept. 15, 2017 1 Arrays in Java int[ ] myInts = new int[15]; myInts[3] = -732; Array whose elements have a primitive type 2 myInts int[ ] myInts = new int[15]; myInts[3] = -732; 0 0 1 0 2 0


  1. COMP 250 Lecture 4 Array lists Sept. 15, 2017 1

  2. Arrays in Java int[ ] myInts = new int[15]; myInts[3] = -732; Array whose elements have a primitive type 2

  3. myInts int[ ] myInts = new int[15]; myInts[3] = -732; 0 0 1 0 2 0 3 -732 : : 0 14 0

  4. Arrays in Java Shape[ ] shapes = new Shape[428]; shapes[293] = new Shape( ); The symbol here corresponds to some arguments that specify a shape. Array whose elements have a reference type 4

  5. int[ ] myInts = new int[15]; Shape[ ] shapes = new Shape[428]; myInts[3] = -732; shapes[293] = new Shape( ); shapes myInts 0 null 1 null null 0 0 null : 1 0 2 0 3 -732 293 : : : 0 null 14 0 427 null 5

  6. The value of a reference variable is an “ address” which specifies where an object is in the computer memory. We often represent a reference with an arrow In the C programming language, you have access to that value and can manipulate it. In Java, you have access to it but you can’t use it. shapes myInts 0 null 1 null null 0 0 null : 1 0 2 0 3 -732 298 : : : 0 null 14 0 427 null 6

  7. Arrays have constant time access A computer accesses an element in an array in constant time i.e. constant, independent of the length N of the array. …. = a[k] ; // read a[k] = …. ; // write You will learn more about how this works in COMP 206 and 273. 7

  8. Arrays versus ‘Array Lists’ Arrays can be used to make lists, sometimes called ‘array lists’. Java has an ArrayList class. 8

  9. List An ordered set of elements , , , … , , is the number of elements in the list, often called the “size” of the list. 9

  10. What things do we do with a list? get(i) // Returns the i-th element (but doesn't remove it) set(i,e) // Replaces the i-th element with e add(i,e) // Inserts element e into the i-th position remove(i) // Removes the i-th element from list remove(e) // Removes first occurrence of element e // from the list (if it is there) clear() // Empties the list. isEmpty() // Returns true if empty, false if not empty. size() // Returns number of elements in the list 10

  11. Lists • array list (today) • singly linked list next week • doubly linked list : 11

  12. array list of int array list of Shape 0 0 4 1 1 -3 2 2 19 3 3 -7 4 4 221 5 5 0 6 6 16 7 7 0 null 8 8 0 null 9 null 9 0 10 0 size = 7 size = 7 length = 11 length = 10 12

  13. Let’s assume that the array is a[ ]. How to implement various operations ? 0 1 2 get(i) { 3 4 if (i >= 0) & (i < size) 5 return a[i] 6 } 7 null 8 null 9 null size = 7 length = 10 13

  14. set(i,e){ // replaces the object at index i if (i >= 0) & (i < size) a[i] = e e.g. set(4, e) } e 0 1 2 3 4 5 6 7 null 8 null 9 null 14

  15. set(i,e){ // replaces the object at index i if (i >= 0) & (i < size) a[i] = e e.g. set(4, e) } e e 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 null null 8 8 null null 9 9 null null 15

  16. add( i, e) Make room by shifting, and then change reference. e.g. add(2, e) e 0 1 2 3 4 5 6 7 null 8 null 9 null 16

  17. add( i, e) Make room by shifting, and then change reference. e.g. add(2, e) e e 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 null 8 8 null null 9 9 null null 17

  18. add( i, e) { // in the figure below, add( 2, e) if (i >=0) & (i <= size){ for (j = size; j > i; j--) a[j] = a[j-1] // shift (copy) a[i] = e // replace value size = size + 1 // increase number of elements } } j 0 j j j j u u u 1 u u e 2 m m m m p m m p p size – 1 y p p y p y y y y 18

  19. add( i, e) { // in the figure below, i = 2 if (i >=0) & (i <= size){ for (j = size; j > i; j--) a[j] = a[j-1] // shift (copy) a[i] = e // replace value size = size + 1 // increase number of elements } } j 0 j j j j u u u 1 u u e 2 m m m m p m m p p size – 1 y p p y p y y y y 19

  20. How to add an element to an array list when array is full ? add( i, e) { // Create an empty bigger array. // Copy all elements to bigger array. // Add new element to the bigger array. } 20

  21. How to add an element to an array list when array is full ? add( i, e) { if (a.size == a.length){ // is array full? make new bigger array b // e.g. b.length = 2*a.length for ( int i=0; i < size; i++) b[i] = a[i] // copy elements to b a = b } // insert the add( i , e ) code from earlier. } 21

  22. SLIDE ADDED What if you want to add an element to the list because you don’t care where it goes? Or what if you want to add an element to the end of the list? The add( i, e) code does not allow this. Instead we need another method add(e ). See Exercises. 22

  23. Overloading add( e ) // inserts element e at end of list add( i ,e) // Inserts element e into the i-th position remove(i) // Removes the i-th element from list remove(e) // Removes first occurrence of element e // from the list (if it is there) 23

  24. Adding N elements to an array list Suppose we initialize an array list with an empty array of length 1. We then add an element. arraylist of arraylist of size 0 size 1 (length 1) (length 1) What do we do to add a second element? add first element

  25. Adding N elements to an array list Suppose each time we add to a full array list, we double the length of the array. arraylist of arraylist of arraylist of size 2 size 1 size 1 (length 2) (length 2) (length 1) add second element

  26. Adding N elements to an array list . arraylist of arraylist of arraylist of size 3 size 2 size 2 (length 4) (length 2) (length 4) add third element

  27. Adding N elements to an array list . arraylist of arraylist of size 4 size 3 (length 4) (length 4) add fourth element

  28. Adding N elements to an array list . arraylist of arraylist of arraylist of size 4 size 5 size 4 (length 8) (length 8) (length 4) add fifth element

  29. Adding N elements to an array list . Double length Double length Double length and copy four and copy one and copy two elements element elements add one element add one element add two elements add four elements 29

  30. Q: How many times do we need to double the length of the array so that it is of length ? A: Q: How many copy operations are required to add elements to an empty array list ? A: 30

  31. Q: How many times do we need to double the length of the array so that it is of length ? 2 = , = A: Q: How many copy operations are required to add elements to an empty array list ? 1 + 2 + 4 + 8 + … 2 = 2 - 1 = A: - 1 31

  32. List Operations get(i) set(i,e) add(i,e) remove(i) // Removes the i-th element from list remove(e) // Removes element e from the list (if it is there) clear() // Empties the list. isEmpty() // Returns true if empty, false if not empty. size() // Returns number of elements in the list : 32

  33. remove( i ) // in the figure below, i = 2 0 j j j j j 1 u u u u u 2 m m m e m 3 m p p p m 4 p y y p p 5 y y y y size = 5 size = 6 33

  34. remove(i) if ( (i >= 0) and (i < size) ){ tmp = a[i] // put aside and later return it for ( k = i; k < size-1; k++){ a[ k ] = a[ k + 1 ] // shift (copy) } size = size – 1 a[ size ] = null // clean return tmp } 34

  35. Quiz 0 : Test your Java skill - Worth 0% of your grade - Starting today at noon until Monday night - Practice mycourses/quiz mechanism and timing - Allow us to test if the system works as we think - Allow you/us to calibrate 35

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend