How to make an addAll() or addRange() method from scratch in java?

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


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>
链接地址: http://www.djcxy.com/p/73922.html

上一篇: 复合比较运算符有什么意义?

下一篇: 如何在java中从头开始创建addAll()或addRange()方法?