2

I'm working on a Linked List project, and i'm having a great trouble with the constructor.

i already implemented the default constructor (creates empty list. AKA data = null, size = 0) but the other constructor is really confusing me !!!!

i want to implement a constructor that creates a linked list with valueS/elementS in it (String[]). My first thought was "Piece of cake, all i have to do is :

  • Use the default constructor to create an empty linked list
  • Use a for-each loop within a for loop. The for-each loop is to iterate the string array and add them to my empty linked list. The for loop is needed to keep a track of the index."

Here is my Code:

public LinkedList(String[] data)
{
    LinkedList l = new LinkedList();

    for (int i = 0; i <= data.length; i++)
    {

        for (String d : data)
        {
            l.add(d, i);
            i++;
        }

    }
}

i tested my code by using this constructor but it does not work. i know there is a silly mistake somewhere but my logic/mind is blind to see it.

6
  • Are you getting some exception? Commented Dec 20, 2014 at 2:11
  • What does "it does not work" actually mean? Commented Dec 20, 2014 at 2:14
  • Why do you need 2 loops? Commented Dec 20, 2014 at 2:15
  • @sol4me Yes man, i should've used this(); instead of using the actual constructor Commented Dec 20, 2014 at 3:51
  • @PM77-1 i created an array of strings, then i created a linked list using my code, then i tried to locate (get method) and check (size method) and they didn't add up. I needed two loops to keep track of the index and to traverse through the array. Commented Dec 20, 2014 at 3:58

4 Answers 4

2

Well you're not really referring to "this" anymore in the constructor you've written. You create a linked list l and modify that one, but you never actually work on "this". Also I agree with the others, the second for loop is unnecessary.

This also lets you use this(), which is a cool functionality to get to know. Helps you keep your code DRY and bug free.

public LinkedList(String[] data){
    this(); //Call the default constructor to set up default properties
    for (String d : data){
        add(d); //Call on this
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

YOU SIR, deserve a pint of your favourite lager on me. I'm so familiar with "this" function, as a matter of fact i've been using it through most (90%) of my other methods. but as i said before "i'm blind to see it" Thank you, (even though i fixed it, i have to deal with other "newborn" bugs.
1

You need to change the arguments to the add method, it expects the index to be the first argument. See http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#add(int,%20E).

    for (String d : data)
    {
        l.add(i, d);
        i++;
    }

1 Comment

That's totally true, but my API writer described the method add as public void add(java.lang.String data, int index). The parameter is data then index.
0

Another way of doing this would be to just add l.add(d) in the loop. With this new elements are guaranteed to be inserted at the end of the list.

I would use l.add(i, d) when I want to insert specifically at a given location.

Comments

0

Does your class extend LinkedList? If so, here is what I would do:

public class MyLinkedList extends LinkedList<String> {

    ...

    public MyLinkedList(String... array) {
        super();
        if (array != null && array.length > 0) {
            for (String s : array) {
                add(s);
            }
        }
    }

    ...

}

It isn't a great idea to extend LinkedList. If you want an easy way to create a new LinkedList with elements use the following method:

public static <E> LinkedList<E> newLinkedList(
        @SuppressWarnings("unchecked") final E... elements) {
    final LinkedList<E> list = new LinkedList<E>();
    Collections.addAll(list, elements);
    return list;
}

....

LinkedList<String> yourList = newLinkedList("foo", "bar", "baz");

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.