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.