I am trying to create a program that will store user input into nodes of a linked list. I want the user to only have to input the data they wish to be stored, so I am trying to figure out a way to automate the naming of the new Node of my linked list.

I tried using a variable in the place of the name of the new Node, like this:

String name = "node";
int counter = 0;
String happy;
for(int i=0; i<10; i++){
    happy = name+counter;
    counter++;
    Node happy = new Node();
}

along with several variations of this idea, none of which worked.

Any creative solutions? Or perhaps some API I am unaware of?

much thanks,
andrewjm

Recommended Answers

All 5 Replies

You can't "dynamically name" variables in Java. I don't know of any language where you can do that, although one might exist. I might be confused by what you're trying to do, but there is a LinkedList class in Java. If you were to use

LinkedList<YourClassnameHere> list = new LinkedList<YourClassnameHere>();

A linked list would be created where you could store and add and remove your Objects from the list, among many other useful functionality. You could simulate the user being able to "store only what they wanted" by simply having a String variable in the Objects that are in the Linked List or something like that.

You may store a value at given key (name) - use Hashtable class.

Why do your Nodes have names? What will you use the name for?

Thanks, all.

I am studying java now in a introductory level course. I was taught to create a linked list from scratch, different from the API it seems. That's where the naming of the Nodes comes into play.

I appreciate the replies, I am looking into each suggestion.

thanks.

I'm not knocking adatapost's suggestion - he gives great suggestions - but his suggestion might be a little too advanced for what you're doing right now. Either way, you might want to explain exactly what your assignment is, because "naming Nodes" doesn't make sense to any of the three people who have commented here and we like to consider ourselves good java programmers :)

commented: I agree. +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.