0

so I am new to programming and having some issues with classes. I am using the lens next brick. There is a class from the libuary called Node. It takes a X and Y. Like so:

Node nodeNameOne = new Node(2,3);

What I want to do:

I have a 12 by 12 array. And for each position in the array I want to get its X and Y and create a new node. The problem is I want to automatically change the " nodeNameOne" .

My solution was to create a intiger variable and increment it, then do a .to string and use that as the nodes name by when I use my intiger variable name it uses the name of the variable not the value within the variable.

I have looked at other posts that sujest using "Class.newInstance" but I can't get this to work.

Any help is appreciated. Thanks

UPDATE:

I have a 12 by 12 array. I will scan the array and when I detect a 0 in any position of the array i want to create a new Node with the X and Y of the array position.

because it is a 12 by 12 array there are 144 possible places that 0 can occur. therefore the code that is needed to make a new is node is: Node nodename = new Node(x,y);

I want to automate the instance name: "nodeName" to use a string variable, that I will get from:

int nameOfNode= 0;
String temp = Integer.toString(nameOfNode);
Node temp = new Node(x,y);
nameOfNode++;

the error i get is that it says that temp is already used, but i know that. i want it to use the value of temp not the name "temp". - i hope this is clearer.

8
  • 1
    Your question is not clear. Why do you (think you) want to change a variable's name? Can you post some example code of what you tried and what does not work? Commented Dec 5, 2016 at 12:25
  • You cannot create variable names dynamically and if you think you have to, then there is something wrong with your concept. Commented Dec 5, 2016 at 12:25
  • thanks, I will upload what I have soo far. Commented Dec 5, 2016 at 12:26
  • i would use a array but like i said i am using the lejo NXT API and to make a Node that will work with a search algorithm i need 3 steps: 1. make nodes. 2. add nodes to mesh. 3. connect the nodes Commented Dec 5, 2016 at 12:32
  • Node one = new Node(1,1); Node two = new Node(1,2); mesh.addNode(one, 0); mesh.addNode(two, 0); mesh.connect(one, two); Commented Dec 5, 2016 at 12:33

2 Answers 2

1

What you want is probably an array. A twodimensional array is a mapping from (i,j) --> Node[i][j]:

// Step 1 and 2: Create and add nodes
Node nodes[12][12];
for (int i = 0; i < 12; i++) {
    for (int j = 0; j < 12; j++) {
        nodes[i][j] = new Node(i, j);
        mesh.add(nodes[i][j], 0);
    }
}

You can then refer to a node by using numbers (or integer variables):

// Step 3: Connect nodes:
mesh.connect(nodes[2][3], nodes[2][4]);
mesh.connect(nodes[2][3], nodes[3][3]);
mesh.connect(nodes[2][4], nodes[2][5]);
...
Sign up to request clarification or add additional context in comments.

2 Comments

i need to create a new instance of a node with a string to reference it by. i do not think this will work
I would find it more convenient to reference the nodes by coordinates (i,j) than by a string. See updated answer.
0

I think you want a java.util.Map;

A Maps is an object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. You can use Strings "x,y" as keys for your nodes objects then you can retrieve your node objects by strings the way you want.

you can use an map like this:

final Map<String,Node> nodesMapedByName = new HashMap<>();
(...)
//asssuming x and y are integers, this will create an 
//new node for x and y and put it in the map using the string
//"x,y" as key
nodesMapedByName.put(x + "," + y, new Node(x,y);
(...)
//asssuming x and y are integers, this will retrieve the node
//mapped by the String "x,y"
final Node someNome =  nodesMapedByName.get(x + "," + y);

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.