1

I have one test input file in which for different scenario different number of object should be created.

For example: for one test input their must be 3 object to create with name v0, v1, v2 while for other test input their must be 5 object to create with name v0, v1, v2 , v3, v4.

For static program of 5 object is given below:

    Vertex v0 = new Vertex("a");
    Vertex v1 = new Vertex("b");
    Vertex v2 = new Vertex("c");
    Vertex v3 = new Vertex("d");
    Vertex v4 = new Vertex("e");
    Vertex v5 = new Vertex("f");

I want to make it dynamic something like this for k=5 (no. of object):

for(int i=0;i<k;i++){
    Vertex vi= new Vertex("str");
}
user2908533
  • 37
  • 1
  • 2
  • 7

3 Answers3

12

What you need is a Map<String, Vertext>

String arr = new String[]{"a", "b", "c", "d", "e", "f"};
Map<String, Vertex> map = new HashMap<>();
for(int i = 0; i < arr.length; i++) {
    map.put("v" + i, new Vertext(arr[i]));
}

Then you can retrieve objects using their names, for example if you need v3 you could just write:

Vertex v3 = map.get("v3"); // returns the object holding the String "d"
System.out.println(v3.somevariable);

If somevariable is holding the string you pass in the constructor then the output of print statement will be

d
A4L
  • 17,353
  • 6
  • 49
  • 70
  • but how it will assign value to object. – user2908533 Oct 22 '13 at 19:19
  • suppose I want to use `v3` like this : v3.somevariable – user2908533 Oct 22 '13 at 19:23
  • after that I want to use like `v0.adjacencies = new Edge[] { new Edge(v1, distance[0][1])` – user2908533 Oct 22 '13 at 19:35
  • @user2908533 you have to do it again in a loop - preferably the same loop as for populating the map -. The adjacent object of the object with the name `"v" + i` is the object with the name `"v" + (i+1)`. You should check the return of the `Map#get` whether it is null or not. It return `null` if no mapping was found. – A4L Oct 22 '13 at 19:40
3

It's impossible to do it in plain Java. You should be able to achieve it somehow using ASM or some byte code manipulation library, but it isn't worth the effort. The best way to do it is to use a Map. Note that Map is an interface, HashMap is its implementation.

String[] names = {"v1", "v2", "v3"};
String[] constructorArgs = {"a", "b", "c"};
Map<String, Vertex> map = new HashMap<String, Vertex>();
for (int i = 0; i < names.length; i++)
{
    map.put(names[i], new Vertex(constructorArgs[i]));
}

for (int i = 0; i < names.length; i++)
{
    Vertex v = map.get(names[i]);
    //do whatever you want with this vertex
}

You can access the variables using their names via map.get(name).

For more info on ASM see this answer.

Community
  • 1
  • 1
Mateusz
  • 3,038
  • 4
  • 27
  • 41
  • after that I want to use like `v0.adjacencies = new Edge[] { new Edge(v1, distance[0][1])` – user2908533 Oct 22 '13 at 19:33
  • You'll have to do it like `map.get("v0").adjacencies = ...`. There is really no way to achieve what you want without manipulating byte code. – Mateusz Oct 22 '13 at 19:36
2

You could use a Map, where the key is a String (name), and the value is the Vertex.

E.g.:Map<String, Vertex>

You could then do this:

Map<String, Vertex> testObjs = new HashMap<String, Vertex>();

for(int i = 0; i < k; i++)
    testObjs.put("v" + String.valueOf(i), new Vertex(i));

// The names would be like v1, v2, etc.
// Access example
testObjs.get("v1").doVertexStuff();
bstempi
  • 2,023
  • 1
  • 15
  • 27