Presentation is loading. Please wait.

Presentation is loading. Please wait.

The ArrayList Data Structure The Most Important Things to Review.

Similar presentations


Presentation on theme: "The ArrayList Data Structure The Most Important Things to Review."— 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 nums = new ArrayList ( ); No square bracket [ ] notation is used for an ArrayList. Note the use of the templating angle brackets 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 nums = new ArrayList ( ); ArrayList names = new ArrayList ( ); ArrayList students = new ArrayList ( ); ArrayList shapes = new ArrayList ( ); Note the empty ( ) parentheses at the end of the constructor call. Don’t forget to include these.

5 The ArrayList Subset of Methods ArrayList methodWhat the Operation Does int size ( )returns the logical size of the list boolean add (E obj)appends the object obj to the end of the list and returns true if successful (the fact that it returns true may be helpful sometimes) void add (int index, E obj)inserts the object obj at position index where index fulfills the expression 0 ≤ index ≤ size. If index is out of bounds in the range of index size(), then Java throws an IndexOutOfBoundsException. Once obj is inserted, then the elements at position index and higher are moved to the right so that 1 is added to their indices and then the logical size is adjusted. Note: if there are 3 elements in the list at indices 0, 1, and 2, then obj can be added at index 3 without an out of bounds error E get (int index)returns the element at position index in the list E set (int index, E obj)replaces the element at position index with obj and returns the element formerly at the specified position E remove (int index)removes the element from position index in the list and then moves all elements at position index + 1 and higher to the left … subtracting 1 from their indices and then adjusting the logical size. The element formerly at the specified position is returned

6 Adding Elements to an ArrayList - add(obj) The following code stores the first 100 multiples of 3 in nums in order: ArrayList nums = new ArrayList ( ); 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


Download ppt "The ArrayList Data Structure The Most Important Things to Review."

Similar presentations


Ads by Google