4

I have the following code so far:

/*
 * This method adds only the items that don’t already exist in the
 * ArrayCollection. If items were added return true, otherwise return false.
 */
public boolean addAll(Collection<? extends E> toBeAdded) {

    // Create a flag to see if any items were added
    boolean stuffAdded = false;


    // Use a for-each loop to go through all of the items in toBeAdded
    for (something : c) {

        // If c is already in the ArrayCollection, continue
        if (this.contains(c)) { continue; }     

            // If c isn’t already in the ArrayCollection, add it
            this.add(c)
            stuffAdded = true;
        }

        return stuffAdded;
    }
}

My question is: what do I replace something (and c) with to make this work?

2
  • this.contains()? is your object implementing Collection ? Commented Jun 8, 2011 at 19:24
  • @Cosmin, probably, as he is implementing addAll here :-) Commented Jun 8, 2011 at 19:54

5 Answers 5

10

Something like this should do:

// Use a for-each loop to go through all of the items in toBeAdded
for (E c : toBeAdded) {

    // If c is already in the ArrayCollection, continue
    if (this.contains(c)) {
        continue;
    }

    // If c isn’t already in the ArrayCollection, add it
    this.add(c);

    stuffAdded = true;
}

The general form is:

for (TypeOfElements iteratorVariable : collectionToBeIteratedOver) `
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need this in front of method invocations.
1

It's pretty simple to write a foreach in Java.

for(ObjectType name : iteratable/array){ name.doSomething() }

You can do foreach with either an iteratable or array. Be aware that if you don't typecheck your iterator (Iterator), then you need to use Object for ObjectType. Otherwise use whatever E is. For example

ArrayList<MyObject> al = getObjects();
for(MyObject obj : al){
  System.out.println(obj.toString());
}

For your case:

   for(E c : toBeAdded){
        // If c is already in the ArrayCollection, continue
        if( this.contains(c) ){ continue;}     

        // If c isn’t already in the ArrayCollection, add it
        this.add(c)
        stuffAdded = true;
    }

1 Comment

*not an itera tor, but an itera ble.
0

E is the collection, c is the variable inside the loop for(E c : toBeAdded ) ...

1 Comment

E is not the collection. E is the type of the variable in the loop.
0
public boolean addAll(Collection<? extends E> toBeAdded) {
    boolean stuffAdded = false;
    for(E c : toBeAdded){
        if(!this.contains(c)){
             this.add(c)
             stuffAdded = true;
        }
    }
    return stuffAdded;
}

Also take a look at Collections.addAll

4 Comments

It is the addAll method he is implementing! :D
Sorry. I just think this is better to use some SET as internal storage.
Are you sure? Even if you worked at Sun and was assigned to implement ArrayList?
No, of course :) I can suppose that there can be implemented collection for this case.
0

You can write the foreach loop in 2 ways and they are equivalent.

List<Integer> ints = Arrays.asList(1,2,3);
int s = 0;
for (int n : ints) { s += n; }

for (Iterator<Integer> it = ints. iterator(); it.hasNext(); ) {
    int n = it.next();
    s += n;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.