Newbie Question: Creating a dynamic object...

T

Taria

Hello all,

Is it possible for Java to generate names within a program and create
an object from that new name? For example, let's say we are given a
set of data to work from and I have a class named NodeInfo which has
information pertaining to this set of data. The number of nodes that
can be generated will depend on the number of elements in the set of
data and can possibly turn into thousands of nodes. (Unlikely in this
school assignment but I'm trying to create something efficient.)

For now, I am tackling this problem like this. When i generate a
child node (which I want to have NodeInfo's class structure), I will
refer to them in order of creation as "node1, node2, node3..etc. So
essentially, in pseudocode:

generate new node name
NodeInfo (newNodeName here) = new NodeInfo();
<data manipulation after this>

Is this possible and if so, what is it called? I have tried to look
online and have found dynamic class loading (I'm pretty sure I'm not
doing that) and various pages that give me info about things I'm also
pretty sure has nothing to do with what I want to do.

An alternative way I've thought of was to create a kind of surrogate
key where I'd create an index of my own associated with the value of
the node within an array structure. I'm at a loss how I would
retreive each node's info though right now. Or maybe even create 5
different arrays and have them referenced in parallel, where I know
that the index of 1 would refer to the first node across the 5
arrays. I tried that earlier but it was very messy.

Anyway, any thoughts on whether using a newly created name as an
object is possible or anything is appreciated (as always.)

-t (the very weary)
 
J

Janusch

generate new node name
NodeInfo (newNodeName here) = new NodeInfo();
<data manipulation after this>

use ArrayList, code for Java 1.4:

list=new ArrayList();

public void generate(){
NodeInfo node = new NodeInfo();
list.add(node);
<data manipulation after this>
}

public void refer(int i){
NodeInfo node = (NodeInfo)list.get(i);
<data manipulation after this>
}
 
T

Taria

use ArrayList, code for Java 1.4:

list=new ArrayList();

public void generate(){
NodeInfo node = new NodeInfo();
list.add(node);
<data manipulation after this>

}

public void refer(int i){
NodeInfo node = (NodeInfo)list.get(i);
<data manipulation after this>



}- Hide quoted text -

- Show quoted text -

Thanks, Janusch. In light that this program was due 2 days ago and
since it's 75% working, I've decided to finish off what's left of it
and try to figure it out the way you have posted above, as an exercise
of my own. I like the way List allows you to reference elements
easily but I already know for myself that it's never a smooth
transition for myself to use a new keyword such as List (much less
understand it fully.) It was like pulling teeth for me to understand
ArrayList as it was.

Thanks a million for your input.
-t
 
L

Lew

Java 1.4 is old. Use Java 6.

There are lots of ways to do what Taria asked. [Array]List was not the most
obvious to me. More obvious were Map and simply creating what you need when
you need it.

Map:

Map <String, Node<Foo>> nodes = new HashMap <String, Node<Foo>> ();
....
Node<Foo> node = nodes.get( name );
if ( node == null )
{
node = new Node<Foo>( new Foo(name) );
nodes.put( name, node );
}
....

Simply creating:

while ( hasMoreToDo() )
{
String name = getName();
Foo foo = new Foo( name );
Node<Foo> node = new Node<Foo> (foo);
doSomething( node );
}

The only real difference between the two is that the Node reference is kept
around in the Map alternative, by dint of being in the Map.

The problem with the List approach is that it requires that you know the index
of the Node, where you asked to find it via the name.

Note that a Map can be indexed by any object type, not just String. You could
key it by Foo, for example, if Foo were a custom object stored in a Node.

Map <Foo, Node<Foo> > nodes = new HashMap <Foo, Node<Foo> > ();
...
Foo foo = getAFoo();
Node<Foo> node = nodes.get( foo );
etc.

<http://java.sun.com/javase/6/docs/api/java/util/Map.html>
 
L

lord.zoltar

I thought that what Taria wanted was a way to dynamically name
variables... at least, that's what it looked like to me. I don't know
of a way to do this in Java so Maps might still be the best way to go.
 
L

Lew

I thought that what Taria wanted was a way to dynamically name
variables... at least, that's what it looked like to me. I don't know
of a way to do this in Java so Maps might still be the best way to go.

You'll note that I set up Foo to have a name, so it's a dynamically-named object.
 
R

Roedy Green

Is it possible for Java to generate names within a program and create
an object from that new name?

You can write code on the fly, compile and run it.

Normally you solve problems of that sort using arrays or collections.
You don't need play with names.

The compiler internally creates a symbol table, look up by name to get
an object. You can do the same thing with a HashMap.

see http://mindprod.com/jgloss/hashmap.html
http://mindprod.com/jgloss/array.html
http://mindprod.com/jgloss/collection.html
 
R

Roedy Green

Is this possible and if so, what is it called?

Instead of trying to create a new tool, tell us the problem. There
will likely be a more Javaesque approach to it.
 
T

Taria

Instead of trying to create a new tool, tell us the problem. There
will likely be a more Javaesque approach to it.

Hello all,

The problem is this, you are given a set of weights with labels
attached to them, (example: A has a weight of 1, B has a weight of 3,
C has a weight of 4, and D has a weight of 2.) Using a bottom up
algorithm such as Huffman code and the shortest path algorithm (listed
in Cormens, we're allowed to use if we want which I did.) find the
shortest path (in weights) and list the minimum value, the full tree
data, and optimal parenthization.

I finished this program, it took a long time for me though and I did
it this way. I generated an arrayList (or List I can't remember right
now) of all possible paths then used the shortest path algorithm to
show me the shortest path in which I generated the optimal
parenthization, etc. I used an ArrayList to hold the diff node
combinations (such as 1342, 442, 172, 136, 46, 82, ....10). I wanted
to create a class that held the different values for each label such
as their previous state, stage, so that I could write my own shortest
path algorithm. But I couldn't! Lol, I tried. I didn't know how to
link the different nodes together once I made them so I went back to
the ArrayList method and referened them by their cells (such as
nodeList.get(i)) I think I struggled more with manipulating data
around with ArrayList more than anything for this assignment.

Anyway one of my brillant ideas was to make a node1, node2 node3
(children of 1342) and somehow link them with their values declared
within the constructors. It sounded good in my head but practically,
as I tried it, it didn't seem work out so I abandoned it.

My next program is to do the same problem but from a top down
approach. o_O I have an idea how to do this and right now, I'd like
to see if I can figure out how to do this without asking for help.
This is supposed to be easier :) And thankfully, the last program has
made me much more comfortable with ArrayLists and such.

For this top down approach I was going to write a recursive algorithm
to match the dynamic programming requirement (which I know is
inefficient but I've been told that to figure it out iteratively is
harder and we're not required to write efficient code.) This is only
to demonstrate the understanding of the top down design concept. I
will probably use ArrayLists to store my values like the last program.

I tried to get famliiar with the Map collections but it was a bit
overwhelming so I went the more manual but easier for me to understand
how to use ArrayList way.

Thanks for all of your input. It helps me something major in this
experience of learning.

-t
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top