Building Java Programs

Lab: ArrayLists

Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.

Lab goals

Goals for this problem set:

ArrayList

ArrayList: Stores an ordered sequence of elements using an array as its internal data structure.

ArrayList

ArrayList Methods

Method Description
list.add(value); appends value at end of list
list.add(index, value); inserts given value just before the given index, shifting subsequent values to the right
list.clear(); removes all elements of the list
list.get(index) returns the value at given index
list.indexOf(value) returns first index where given value is found in list (-1 if not found)
list.remove(index); removes/returns value at given index, shifting subsequent values to the left
list.set(index, value); replaces value at given index with given value
list.size() returns the number of elements in list
list.toString() returns a string representation of the list such as "[3, 42, -7, 15]"

Exercise : ArrayList declaration syntax practice-it

Which of the following choices is the correct syntax for declaring/initializing an ArrayList of integers?

Exercise : mystery practice-it

Write the output produced when passing each ArrayList to the following method:

public static void mystery(ArrayList<Integer> list) { 
    for (int i = 0; i < list.size(); i += 2) { 
        int element = list.get(i); 
        list.remove(i); 
        list.add(element); 
    } 
    System.out.println(list); 
}
[2, 4, 6, 8]                        // [4, 6, 2, 8]
[10, 20, 30, 40, 50, 60]            // [20, 30, 50, 60, 40, 10]
[-4, 16, 9, 1, 64, 25, 36, 4, 49]   // [16, 9, 64, 25, 4, 49, 1, 36, -4]

Exercise : mystery2 practice-it

Write the output produced when passing each ArrayList to the following method:

public static void mystery2(ArrayList<Integer> list) {
    for (int i = list.size() - 1; i >= 0; i--) {
        if (i % 2 == 0) {
            list.add(list.get(i));
        } else {
            list.add(0, list.get(i));
        }
    }
    System.out.println(list);
}
[10, 20, 30]             // [20, 10, 20, 30, 30, 20]
[8, 2, 9, 7, 4]          // [8, 7, 8, 2, 9, 7, 4, 4, 2, 8]
[-1, 3, 28, 17, 9, 33]   // [33, 28, 33, -1, 3, 28, 17, 9, 33, 17, -1, 33]

Exercise : Debugger

Exercise - Debugger

Can you figure out the bug in the program? Modify the code so that it will remove ALL words that end in "s".

Recall: ArrayList as Parameter

public static void name(ArrayList<Type> name) {

Example:

// Prints all plural words from the given list.
public static void printPlural(ArrayList<String> list) {
    for (int i = 0; i < list.size(); i++) {
        String str = list.get(i);
        if (str.endsWith("s")) {
            System.out.println(str);
        }
    }
}

Exercise : acronymFor practice-it

Write a method called acronymFor that takes an ArrayList of strings as a parameter and that returns an acronym made by combining the capitalized first letter of each word in the list.

For example, the list [laughing, out, loud] produces the acronym "LOL".

Exercise : addStars

Write a method called addStars that accepts an ArrayList of strings as a parameter and places a "*" after each element.

For example, if an list initially stores [the, quick, brown, fox], you should modify it to store [the, *, quick, *, brown, *, fox, *].

Exercise : switchPairs practice-it

Write a method called switchPairs that switches the order of values in an ArrayList of strings in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. If there are an odd number of values in the list, the final element is not moved.

For example, if a list stores [to, be, or, not, to, be, hamlet], you should modify it to store [be, to, not, or, be, to, hamlet].

Exercise : reverse3 practice-it

Write a method called reverse3 that accepts an ArrayList of integer values as a parameter and that reverses each successive sequence of three values in the list. If the list has extra values that are not part of a sequence of three, those values are unchanged. For example, the list:

should be modified to store:

Exercise : intersect practice-it

Write a method called intersect that accepts two sorted ArrayLists of integers as parameters and returns a new list that contains only the elements that are found in both lists.

For example, if lists named list1 and list2 initially store:

Then the call of intersect(list1, list2) returns the new list: