1

I am trying to "convert" this code to a method that will create node items. I know that I have to use a for loop but I cant figure out a way to get this done.

Original Code:

public class GenericLinkedListDemo
{
 public static void main(String[] args)
 {
 LinkedList3<Entry> list = new LinkedList3<Entry>( );
 Entry entry1 = new Entry(1);
 list.addToStart(entry1);
 Entry entry2 = new Entry(2);
 list.addToStart(entry2);
 Entry entry3 = new Entry(3);
 list.addToStart(entry3);

}

What I did until now was create a method in the GenericLinkedListDemo that sends the parameter:

public class GenericLinkedListDemo
    {
     public static void main(String[] args)
     {
     LinkedList3<Entry> list = new LinkedList3<Entry>( );
    addToList(list, 7);

to my method:

public static void addToList(LinkMaster<Entry> L, int n){
        for (int i = n; i>0; i--) { 
            //This is where I want to put my "converted code"
        }
    }

I already did all the methods to create the node(LinkMaster). I just want to know how to make this piece of code above works in the way that I just need to send a parameter to the code.

2
  • What is addToList supposed to do? What are you expecting to be added to the list by this method? Commented Nov 12, 2015 at 4:00
  • In the original code I need to enter with entry for each node that I want to create(entry1, entry2,entry3). What I want is a for loop that will make the same process (create nodes). Commented Nov 12, 2015 at 4:05

1 Answer 1

1

I guess you want something like this

public static void addToList(LinkMaster<Entry> list, int n){//here n will determine number of entry node to be added
        for (int i = n; i>0; i--) { 
            Entry entry = new Entry(i);
            list.addToStart(entry);
        }
    }

If you pass n = 7 ,then 7 entry node will be added to the list.

Sign up to request clarification or add additional context in comments.

1 Comment

It's exactly what I want. I did the changes and eclipse gives me the following message: list cannot be resolved and The local variable list may be not initialized. I have been trying this the whole night.

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.