Limited access

Upgrade to access all content for this subject

Which code segment will take an array list of dog types and modify the element to add
"dog" to the end of the type if there is no "dog" in the dog type?

For example, "collie" becomes "collie dog", and "hound dog" remains as "hound dog".

ArrayList<String> dogs = // a list full of dog types
A
for (int i=0; i<dogs.size(); i++)
{
    if (dogs.get(i).indexOf("dog") == -1)
        dogs.set(i, dogs.get(i) + " dog");
}
B
for (int i=0; i<dogs.size(); i++)
{
    if (dogs[i].indexOf("dog") == -1)
        dogs[i] += " dog");
}
C
for (int i=0; i<dogs.size(); i++)
{
    if (dogs.indexOf("dog") < 0)
        dogs.add(i, dogs.get(i) + " dog");
}
D
for (int i=0; i<dogs.size(); i++)
{
    if (dogs.get(i).indexOf("dog") <= 0)
        dogs.set(i, dogs.get(i) + " dog");
}
E
for (int i=0; i<dogs.size(); i++)
{
    if (dogs.get(i).contains("dog"))
        dogs.set(i, dogs.get(i) + " dog");
}
Select an assignment template