1

I'm trying to do a linkedlist for an assigment i have, this ask explicitly to create, from scratch a linkedlist and some derivated types like a queue and a stack, this is just some college homework, and i realize how to make a node class and a linkedlist class, but i'm struggling to create the addAll() method in this linkedlist class, this is what i have.

if i must bet, i say is the Collection c one, but then, i'm trying to add list of stuff there, in order to pass him's content to the new list, obiusly is not ready and obiusly doesn't work.

Can you tell me how i can pass some kind of "proto-list" in order to pass them data inside the new list?

(I know i must use somekind of for(objects) but i'm failing to pass some data through the parameter, which will be the right parameter to put there?)

public boolean addAll(Collection c) {
        for (int i = 0; i < (this.listaNodos.size()); i++) {

            //for (T someT : c){
            //  Node newNodo = new Node(someT);
            //}

            //i know the one down there is not gonna do anything, because
            //i'm not accesing the data, but one problem at a time would ya ;)
            Node newNodo = new Node(someT);
            Node actualNodo = this;
            boolean processFinished = false;

            try{
                if(index >= this.listaNodos.size() || index < 0){
                    throw new IndexOutOfBoundsException();
                }


                do{
                    if(index == actualNodo.getIndex())
                    {
                        actualNodo.setData(someT);
                        processFinished = true;
                        return true;
                    }
                    else
                    {
                        actualNodo = actualNodo.nextNode;
                    }
                }while(!processFinished);
                return false;
            }catch(IndexOutOfBoundsException ex)
            {
                throw ex;
            }
        }
        return false;
    }

Can you tell me how to fix it to make it work?

Any request for clarification, constructive comment, or question would be greatly apreciated too.

Thanks in advance

user57129
  • 309
  • 1
  • 6
  • 17
  • Don't you have an add() method in your list? Why don't you just iterate on the collection and call `this.add()` for each element? – JB Nizet Nov 20 '16 at 18:34

1 Answers1

1

I assume you already have an add() method of some sort right? If so, you can go over each element in c and add it using the add method:

public boolean addAll(Collection<T> c) {
    boolean changed = false;
    for (T t:c) {
        changed |= this.add(t);
    } 
    return changed;
}

I'm assuming the returned boolean means whether this list has changed, this is how it is defined in the Collection contract: https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html#addAll(java.util.Collection).

You were also missing a generic type for your add method, so I added one. I assume your class definition looks somthing like this?

public class MyLinkedList<T>
Todd Sewell
  • 1,444
  • 12
  • 26
  • Boy, you rock!, tell me what does/means '|='? – user57129 Nov 20 '16 at 19:43
  • Ah it's a shorthand for "or equals". The code `a |= b` is equivalent to `a = a || b`. See [this question](http://stackoverflow.com/questions/2486472/shortcut-or-assignment-operator-in-java) for more info. – Todd Sewell Nov 20 '16 at 19:48