-3
public class Node<E>{
    Node p,l,r;
    int height;
    String s;

    /** class body**/

};

String[] S=new String[5000];
int i = 0;
while (i < 5000){
   Node<E> x=new Node<E>();
   x.s=S[i];
   i++;
}

I want to make 5000 Node objects. above code assign same variable name x every time but i want different variable name. then how to declare 5000 class variable name without declaring it manually. is there something by which i can create 5000 Node class object with ease.

3 Answers3

1

In Java: If you don't want to change the size of the array, you can use one. Otherwise, you can use a dynamic array, like an ArrayList:

int size = 5000;
Node[] S = new Node[size];

for (int i = 0; i < size; i++) {
    S[i] = new Node();
}

Edit: Assigning a name dinamically in Java is not possible. But, using the method above, you can access to the elements by

S[index]

where index will be in the range [0,size-1]

Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

Java: I would suggest making a Node Array with a size of 5000. This would allow you to loop through this array, placing any value inside each individual Node.

public void node() {
    Node[] allNodes = new Node[5000];

    //Print out all node values
    for (Node currentNode : allNodes) {
        currentNode.setNodeValue("Some value");
        System.out.println("Name: " + currentNode.getNodeValue());
    }
}

In this example, I am creating a Node array, with a size of 5000. Then, I use an enhanced for loop to loop through the array. I set every node value in the "allNodes" array to "Some value", then I print it.

When you need to access those values again, just create another loop to get each value.

Edit: You cannot "mass name variables" in Java. You can create arrays which store multiple objects, but they don't have specific names. You access array items by pointing to an index in the array

Walker Christie
  • 531
  • 1
  • 3
  • 18
  • its work in the case of non generic class but not work in class with generic type .Is there other way for generic class or a way to convert generic class to non generic class.please help – user3717895 Jun 08 '14 at 03:47
  • I'm a bit confused as to what you are asking.. Are you saying that this will only work in the case of a non-generic class? This code doesn't interfere with any other classes except for Node. – Walker Christie Jun 08 '14 at 03:51
  • @ walker christie my node class is Node and error occur while running your code is that can't make a list generic type object; – user3717895 Jun 08 '14 at 03:56
  • Why is your node class generic? I would highly suggest making it non-generic. – Walker Christie Jun 08 '14 at 04:08
0

We can't do this without using an array because there is no way to change the name of the variable.

Make 5000 Node objects by creating Node array Node[] x = new Node[size];

Please refer this code snippet

    int size  = 5000;

    String[] S = new String[size];
    int i = 0;
    Node[] x = new Node[size];
    while (i < 5000) {
        x[i].s = S[i];
        i++;

    }