0

I have implemented a simple linked list class and I would now like to use it in a loop. I am wondering how to best assign names to the list in each iteration of the loop.

Essentially I am looping over some integers, and I would like to just give each list the name of that integer, but I cannot say

List i = new List();

right ?

There probably is an easy way to do this, but I m not sure how, and would be grateful for

2
  • Are you creating more than one list? Commented Feb 10, 2012 at 0:08
  • yes, up to 100. Essentially I m trying to read in a Graph and save it in adjacency lists implemented as linked lists. Commented Feb 10, 2012 at 0:10

2 Answers 2

3

I think you're confusing the role of variables with the role of collections. From your question I gather that you want to create a list for each index in your loop, and you would like to later be able to access that list by its index:

ArrayList<LinkedList<String>> listOfLists = new ArrayList<LinkedList<String>>();
for(int i = 0; i < 10; i++)
{
    LinkedList<String> list = new LinkedList();
    listOfLists.add(list);
    // do stuff to the list...
}
// access
LinkedList<String> thirdList = listOfLists.get(2); // index 2 = third entry

So you see, the LinkedLists are not named according to the value of i, but you can still access them by a given value of i.

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

Comments

1

First of all, if if not for learning purposes, it is highly recommended not to implement your own classes for stuff that is already implemented in libraries.

For Lists, you should check out the collection framework: http://docs.oracle.com/javase/tutorial/collections/

I am not sure what you mean by "the name of that integer".

I assume you want to create a List of elements that contain both an integer, and a String representing the name of the value that is hold in the integer.

If it is the case, the best way to do this probably is to create your own Object:

class NamedInteger {
    private int value;
    private String name;

    public NamedInteger(int value, String name) {
        this.value = value;
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void setName(String name) {
        this.name = name;
    }
}

The advantage of this method, is that later, if you want to add other information to your object, it is very easy to do so.

And then, just have a List of those objects....

public static void main(String[] args) {
    List<NamedInteger> list = new LinkedList<NamedInteger>();
    list.add(new NamedInteger(1, "Hello");
    ...
} 

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.