SP6
SP6

Reputation: 119

java programming - dynamic variable names

I had a question regarding this program I was trying to write.

fin = new FileInputStream (args[0]);
            BufferedReader d = new BufferedReader(new InputStreamReader(fin));
            String str;

            while ((str = d.readLine()) != null) {
                String [] string = str.split(" ");
                System.out.println(string[0]);
                int i=0;
                if(!(string[0].compareTo("INPUT")==0) ||  (string[0].compareTo("OUTPUT")==0))
                {
                    // solution goes here

                }
            }

            fin.close();        

So the question i have is that i am trying to create objects a1, a2, a3 etc of a class (say A) and then add them as a vertex to a graph. Is there a way to define them dynamically? Would this work?

A [] a;
while(condition is true)
{
 a[i].setParameters() // dont worry about this
 graph.addVertex(a[i])
 i++;
}

If not, can you tell me a better way of doing this? When i try this i get local variable not initialized error. I might be missing something trivial

Note: The project is to design a logic circuit simulator and I am trying to implement a graph styled data structure where nodes depict gates and the interconnects will be edges (I have two classes called gates and interconnects)

Upvotes: 0

Views: 211

Answers (3)

dsolimano
dsolimano

Reputation: 8986

It looks like you need to initialize the array a. Instead of

A [] a;

you need

A[] a = new A[100];//whatever size you need.

If you don't know how many As you will be creating, some sort of dynamically expanding structure like a List might be more appropriate.

List<A> listOfA = new ArrayList<A>();
while(condition is true)
{
  A a = createASomehow();
  a.setParameters();
  listOfA.add(a);
  graph.addVertex(a)
}

Though really, I'm not sure quite why you need to keep the set of As on the side, does the graph not have some method that returns a list of its vertices?

Upvotes: 1

b.buchhold
b.buchhold

Reputation: 3896

It is hard to tell if this fixes all of your problems, since you didn't provide a precise error message. At least it resolves a problem. You never created objects in your array. You'd have to know the since upfornt to do so, or you can achieve the desired behavior just like this:

while(condition is true)
{
 A a = new A();
 a.setParameters(); // dont worry about this
 graph.addVertex(a);
 // If you also need it in some kind of array, add it to the array, too.
 // Might be nice to use Vector<A> or List<A> aList = new ArrayList<A>() 
 // since those two can outmaticcaly grow while inserting at the back.
 // Create those outside of the loop if you need them at all.
 i++;
}

Upvotes: 2

Brian Roach
Brian Roach

Reputation: 76888

Problem #1:

A [] a;

This only declares a. You still have to create the array with:

a = new A[5]; // Or at the same time with: A[] a = new A[5];

for example, to get an array that can hold 5 A objects. Then each element needs to be created:

a[0] = new A();
...

Problem #2: As you can see, arrays aren't dynamic, which is problematic if you don't know how large they need to be ahead of time. You need to look at perhaps using a collection, such as an ArrayList

Upvotes: 1

Related Questions