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;
    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,

7 Years
Discussion Span
Last Post by BestJewSinceJC

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.


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.



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 :)

Votes + Comments
I agree.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.