160 likes | 446 Views
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.
E N D
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. 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.
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.
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.
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);
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]);
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.
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++; }
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 } }
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.
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
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