Searching Arrays / ArrayLists

Whilst some of the methods available on ArrayList can be useful for locating objects within the array, they do not work in every circumstance, for example if we wanted to check if a list of integers contained a number that was over 100, or a multiple of seven.

The simple solution is to iterate through the collection. If we are just checking for the existance of something, we can exit the loop as soon as we find it, or, if we are looking for all the items that match some criteria then we can copy them to a new array.

Example - Finding something

This example searches an ArrayList of numbers and changes the value of a boolean to true if one of the numbers is wholly divisable by 10. The user would then likely create an if statement that checks the boolean value and provide further code as required.

//assumes an array of Integers called intArrayList already exists

boolean arrayContainsAMultipleOfTen = false;
for (Integer number: intArrayList) {
    if (number % 10 == 0) {
        arrayContainsAMultipleOfTen = true;
        break; //exit loop as there's no need to keep searching

Example - filtering the list

In the following example we are looking for strings that start with the letter 'A', and populating a second array with only the values we find that match

//assumes stringArrayList is an ArrayList containing Strings
ArrayList<String> matchedWords = new ArrayList<String>();

for (String word:stringArrayList) {
    if (word.startsWith("A")){

//matchedWords will now contain any words from the original array starting with 'A'

Deleting items - how not to do it

Removing items from an Array using a method similar to the above examples is to be avoided. Consider the following code:

ArrayList<String> namesList = new ArrayList<String>();

for (String word:namesList) {
    if (word.startsWith("A")){
        namesList.remove(word); //will crash here
If we attempt to execute this code, it will error, with a ConcurrentModificationException, essentially because it doesn't like us modifying something as we are looping through it's items - imagine walking up a flight of stairs and trying to remove a stair as you are stepping on it. To confuse matters somewhat, sometimes Java will sometimes allow us to remove an item from a collection we are iterating through with a for or foreach loop without erroring, however such code should be avoided. The Java Docs for collections (an ArrayList is a type of collection) state that there is only one safe way to modify a collection during iteration, and that is to use an iterator.

See the Iterators tutorial for how to delete items from an ArrayList (or other form of collection) using an iterator.

Task - valid tweets

Create a method that will take an ArrayList of Strings and return a new ArrayList of Strings containing only those that would fit in a tweet (e.g. 140 characters or less). Create unit tests to test your method.

Further Task

Implement the method below as described in the comments. Assume that endNumber must be in the range 0 - 9. Create unit tests to verify it

public ArrayList<Integer> numbersEndingWithValue(ArrayList<Integer> numberList, int endNumber){
    //e.g. If numberList contained 648, 134, 8, 17, 98 and 45 and endNumber was 8
    //this method should return an ArrayList containing 648, 8 and 98

Modify the method, such that endNumber can be any number of digits long. Test the method.