Newbie Question: Creating a dynamic object...

Discussion in 'Java' started by Taria, Nov 22, 2007.

  1. Taria

    Taria Guest

    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)
    Taria, Nov 22, 2007
    #1
    1. Advertising

  2. Taria

    Janusch Guest

    Re: Newbie Question: Creating a dynamic object...

    On 22 Nov., 11:57, Taria <> wrote:
    > 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>
    }
    Janusch, Nov 22, 2007
    #2
    1. Advertising

  3. Taria

    Taria Guest

    Re: Newbie Question: Creating a dynamic object...

    On Nov 22, 2:55 am, Janusch <> wrote:
    > On 22 Nov., 11:57, Taria <> wrote:
    >
    > > 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>
    >
    >
    >
    > }- 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
    Taria, Nov 22, 2007
    #3
  4. Taria

    Lew Guest

    Re: Newbie Question: Creating a dynamic object...

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


    Janusch wrote:
    >> use ArrayList, code for Java 1.4:


    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>

    --
    Lew
    Lew, Nov 22, 2007
    #4
  5. Taria

    Guest

    Re: Newbie Question: Creating a dynamic object...

    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.
    , Nov 22, 2007
    #5
  6. Taria

    Lew Guest

    Re: Newbie Question: Creating a dynamic object...

    wrote:
    > 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.

    --
    Lew
    Lew, Nov 22, 2007
    #6
  7. Taria

    Roedy Green Guest

    On Thu, 22 Nov 2007 02:57:43 -0800 (PST), Taria <>
    wrote, quoted or indirectly quoted someone who said :

    >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
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 23, 2007
    #7
  8. Taria

    Roedy Green Guest

    On Thu, 22 Nov 2007 02:57:43 -0800 (PST), Taria <>
    wrote, quoted or indirectly quoted someone who said :

    >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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 23, 2007
    #8
  9. Taria

    Taria Guest

    Re: Newbie Question: Creating a dynamic object...

    On Nov 22, 6:49 pm, Roedy Green <>
    wrote:
    > On Thu, 22 Nov 2007 02:57:43 -0800 (PST), Taria <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >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.
    > --
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    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
    Taria, Nov 24, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maziar Aflatoun

    Creating object of another object

    Maziar Aflatoun, Apr 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    301
    Kevin Spencer
    Apr 6, 2005
  2. Jesper Sahner
    Replies:
    4
    Views:
    483
    Boudewijn Dijkstra
    Jan 18, 2005
  3. Jess
    Replies:
    10
    Views:
    494
    James Kanze
    May 18, 2007
  4. Greg Hoover
    Replies:
    1
    Views:
    479
    Robert Bossy
    Mar 25, 2008
  5. Paul Childs
    Replies:
    1
    Views:
    314
    Matimus
    Jun 3, 2008
Loading...

Share This Page