1 / 13

The ArrayList Data Structure

The ArrayList Data Structure. The Most Important Things to Review. The Java ArrayList Class. An ArrayList is a one-dimensional array. An ArrayList holds only objects , not primitive values like int, double, char or boolean.

truong
Download Presentation

The ArrayList Data Structure

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. The ArrayList Data Structure The Most Important Things to Review

  2. The Java ArrayList Class An ArrayList is a one-dimensional array. An ArrayList holds only objects, not primitive values like int, double, char or boolean. The ArrayList will be created for a beginning amount and then it will automatically resize itself when it becomes full and more elements need to be added.

  3. Instantiating an ArrayList Declaring and instantiating an ArrayList: ArrayList <Double> nums = new ArrayList <Double> ( ); No square bracket [ ] notation is used for an ArrayList. Note the use of the templating angle brackets < and > that enclose the type of object to be placed in the array. Instead of using [] to manipulate elements, methods are called to perform operations. An ArrayList does have indices and the positions of the elements range from index 0 to index logical size - 1.

  4. Instantiating an ArrayList Declaring and instantiating an ArrayList of other types: ArrayList <Integer> nums = new ArrayList <Integer> ( ); ArrayList <String> names = new ArrayList <String> ( ); ArrayList <Student> students = new ArrayList <Student> ( ); ArrayList <Shape> shapes = new ArrayList <Shape> ( ); Note the empty ( ) parentheses at the end of the constructor call.Don’t forget to include these.

  5. The ArrayList Subset of Methods

  6. Adding Elements to an ArrayList - add(obj) The following code stores the first 100 multiples of 3 in nums in order: ArrayList <Integer> nums = new ArrayList <Integer> ( ); int value = 3; for (int i = 0; i < 100; i++) { nums.add(value); // adds at the end of the list value += 3; } Note: if nums was a standard array, we would use … nums [i] = value; in place of nums.add(value);

  7. Getting Elements from an ArrayList - get(i) The following code prints the values stored in nums with 10 values per line with a field width of 5: for (int i = 0; i < nums.size(); i++) { if ( (i + 1) % 10 == 0 ) System.out.printf( “%5d%n”, nums.get(i) ); else System.out.printf( “%5d”, nums.get(i) ); } Note: if nums was a standard array, we would use … System.out.printf( “%5d”, nums[i]);

  8. Getting Elements from an ArrayList - get(i) To obtain the first element in an ArrayList always use … nums.get(0); To obtain the last element in an ArrayList always use … nums.get(nums.size() - 1); Note that since there are num.size() elements in the ArrayList, then the index of the first element is 0 and the index of the last element is nums.size() - 1.

  9. Removing Elements from a Position - remove(i) The following code removes and prints all of the multiples of 6 from nums in a field width of 5: int i = 0; while ( i < nums.size()) { if ( nums.get(i) % 6 == 0 ) { int x = nums.remove(i) ; System.out.printf( “%5d”, x ); // don’t increment i because elements are shifted down } else // if not evenly divisible by 6 increment i i++; }

  10. Removing Elements from a Position - remove(i) The following code removes and prints all of the multiples of 6 from nums in a field width of 5 using a for loop: for (int i = 0; i < nums.size() ; i++) { if ( nums.get(i) % 6 == 0 ) { int x = nums.remove(i) ; System.out.printf( “%5d”, x ); i--; // to cancel the effect of i++ in loop header } }

  11. Removing Elements from a Position - remove(i) To delete the first element in an ArrayList always use … nums.remove(0); To delete the last element in an ArrayList always use … nums. remove(nums.size() - 1); Again, since there are num.size() elements in the ArrayList, then the index of the first element is 0 and the index of the last element is nums.size() - 1.

  12. Adding Elements at a Position - add(i, obj) The following code adds the integer 3 at the first of the list named nums using the add(i, obj) method, where the list initially contains 6, 9, 12. nums.add (0, 3);// add 3 at index 0. The list now contains: 3 6 9 12 The following code adds the integer 15 at the end of the list no matter how many elements there are using the add(i, obj) method. nums.add (nums.size() , 15);// add 15 after the last element Since nums.size() is 4 before adding 15, then 15 is added at index 4. Obviously 3, 6, 9, &12 are in indices 0, 1, 2, & 3. The list now contains: 3 6 9 12 15

  13. Replacing Elements at a Position - set(i, obj) Assume nums contains: 3 6 9 12 15 The following code replaces the element at index 2 with the value 10 using the set(i, obj) method. It returns the replaced element and prints it out. int x = nums.set (2, 10);// replace 9 at index 2 with 10. System.out.println(“The replaced value was ” + x); The list now contains: 3 6 10 12 15 Replacing the last value in the ArrayList … int x = nums.set (nums.size() - 1, 20); The list now contains: 3 6 10 12 20

More Related