Results 1 to 5 of 5
  1. #1
    grey is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default Please could anyone help trying to figure it out? Want to instance different objects

    Hi all,
    I'm getting troubles on trying to instance a class into a for doing this:

    for (int n = 0; n < PilaAuxiliar.Longitud(); n++){

    String MyStackName = "p" + n; // This is to define the stack's name as P1...n
    ClasePila MyStackName = new ClasePila(int_elementos_temp);

    }

    As you can see I want to instance as many stacks as n values I have, but when I tryo to compile it java says me that I'm trying to MyStackName repeated to the String name and the object name...:O

    How can I iterate and assign the name to the object as an incremental number.

    In my programm people will introduce how many stacks the are gonna use and then I have to create this quantity of stacks to be manipulated.

    Thanks !
    Grey

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    828
    Rep Power
    6

    Default

    Hi,
    Send a pseudocode of what u want to do first.Based on this people can give suggestions.First of all where u are using "MyStackName" variable?

    -Regards
    Ramya
    Ramya:cool:

  3. #3
    grey is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default

    "MyStackName" is a variable created just into the For loop, and then used to say that I want to create an instance of ClassPila called "MyStackName" because of "MyStackName" has value "P1" on first iteration then will have value "P2" and so on....

    Will try to create a pseudo code though

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by grey View Post
    Java Code:
    for (int n = 0; n < PilaAuxiliar.Longitud(); n++){
      String MyStackName    = "p" + n; // This is to define the stack's name as P1...n
      ClasePila MyStackName = new ClasePila(int_elementos_temp);
    }
    As you can see I want to instance as many stacks as n values I have, but when I tryo to compile it java says me that I'm trying to MyStackName repeated to the String name and the object name...:O
    How can I iterate and assign the name to the object as an incremental number.
    You can't do this in Java.

    In my programm people will introduce how many stacks the are gonna use and then I have to create this quantity of stacks to be manipulated.
    Consider perhaps using a HashMap<String, ClasePila> object to hold your "stacks" and to tie each stack to a specific String. Other options include using an ArrayList or array if you want to associate a stack with a number.

  5. #5
    grey is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks I will try with ArrayList and Hash. That make sense!

    Grey

Similar Threads

  1. Array of objects, as an Instance variable
    By blaklite in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 12:56 AM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. Replies: 2
    Last Post: 11-08-2008, 04:58 AM
  4. Replies: 2
    Last Post: 07-11-2008, 09:41 AM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •