-1

I am writing a Linked List class that takes in names or numbers, and then prints them out in a list. I managed to write the list normally. Here is what I did:

public String toString(){
    return list.toString; //where list is the LinkedList I am calling
}

That works correctly and returns my list after adding 4 elements like this:

[Joe, Jessica, Max, 5]

Now I am trying to convert that same method onto a generic method, so I did 2 things.

Here I created the collections object:

private Collection<E> collection;

public MyLinkedListG(Collection<E> _collection) {
    collection= _collection;
}

And here is how I wrote the new toString in collections:

 public String toString(){
    StringBuilder builder = new StringBuilder();
    for(E e : collection) {
        builder.append(e); //appends each string
    }
    return builder.toString();
}

The problem is that now my test class will not allow me to call the LinkedList object I had created before which was:

 MyLinkedListG x = new MyLinkedListG();

It states I need to input a collection inside the parameter. How can I call it now? Or am I doing it totally wrong?

If something is not clear please let me know so I can clarify as soon as possible. Thanks in advanced.

user1404664
  • 73
  • 1
  • 2
  • 8
  • Well yeah, you just created a single constructor that takes an argument.. Obviously you'll have to provide a collection in there – Jeroen Vannevel Jan 09 '14 at 04:30
  • First of all, your constructor is flawed since it takes in a random collection (which could be a set or priority queue or whatever) - which you cannot directly use as a backing collection for your custom linked-list. A better solution is to have a no-argument constructor, and expose a method to **add** a single element to the linked-list. Or if you must have the constructor, let the parameter type be a java.util.List and not a java.util.Collection – aquaraga Jan 09 '14 at 04:32
  • @JeroenVannevel Let's say I want to add several values onto the list, how would I add them? Does it have to be through the argument? – user1404664 Jan 09 '14 at 04:33
  • @aquaraga But I want it to take both ints and Strings, how would I make it so that it accepts both? – user1404664 Jan 09 '14 at 04:34
  • @user1404664 If so, don't use generics. Generics are meant to handle collections of the same type. Or, treat your underlying collection as a List – aquaraga Jan 09 '14 at 04:37
  • @aquaraga oh that makes sense, I couldn't understand how to use both types. That explains it, thanks. – user1404664 Jan 09 '14 at 04:42

2 Answers2

0

You can only use an empty constructor if

A) you have not defined a constructor

or

B) if you have explicitly defined an empty constructor.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9

BevynQ
  • 8,089
  • 4
  • 25
  • 37
0

From what I can tell, your original class likely did not include a constructor. This means the no-arguments constructor new MyLinkedListG() is provided by default, which is likely what you used to construct an instance of your class.

After your modifications, you added a constructor MyLinkedListG(Collection<E> _collection). Now the no-arguments constructor is not provided by default anymore. If you want to continue to use it, it must be explicitly defined.

Your class will probably have two (or more) constructors in this case, perhaps something like this:

private Collection<E> collection;

public MyLinkedListG(Collection<E> _collection) {
    collection= _collection;
}

public MyLinkedListG() {
    collection=new LinkedList<E>();
}

Now you can use either constructor for your object.

Krease
  • 15,805
  • 8
  • 54
  • 86