Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Using an existing char variables stored value to call a method on a vector?

  1. #1
    itslikeanegg is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Using an existing char variables stored value to call a method on a vector?

    Hey guys, here's the problem I have. I'm writing a Towers of Hanoi problem for a course and I have the recursive solution already complete. What I'm trying to do is visually show the user which number disk is on which poll for every loop.

    A.........B.........C
    123
    A.........B.........C
    23.......1
    ...and so on until....
    A.........B.........C
    ......................123

    The way I'm doing it is I have three vectors named A,B,C and A is initialized with the 1,2,3 disks. For every pass of the recursive solution I need to do a .remove(disknum) and a .add(disknum) on the correct vector but of course every pass moves a different disk from a different poll. Now the recursive solution is actually fed the fromPeg and toPeg for every pass and I need something that will work like this:

    fromPeg.remove(diskNum); toPeg.add(diskNum) but to actually operate on one of my A, B, C vectors representing those polls (as if I was calling A.remove(diskNum); B.add(diskNum)). The value stored by char toPeg and char fromPeg would be either A, B, or C.

    How would I go about doing this?

    Right now the output is:
    Disk 1 from A to C
    Disk 2 from A to B
    Disk 1 from C to B
    Disk 3 from A to C
    Disk 1 from B to A
    Disk 2 from B to C
    Disk 1 from A to C
    Last edited by itslikeanegg; 09-09-2011 at 02:54 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Using an existing char variables stored value to call a method on a vector?

    You posted a lot of details (in comparison to most who provide zero details) but I'm not sure what your problem is.

  3. #3
    itslikeanegg is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Using an existing char variables stored value to call a method on a vector?

    Basically I need to use the value of a char to perform an operation on a Vector which has the same name as the char's value.

    So instead of calling a vector, A.add(x); I need something equivalent to using the value inside of a char, toPeg.charValue.add(x); in effect using the chars value to call the vector. I need this because the value inside the char is changing with every loop and it dictates which vector needs to be operated on (although the char variables name is static)

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Using an existing char variables stored value to call a method on a vector?

    It still does not make sense to me and I get the feeling you are going about this the wrong way but why not use an if statement?
    Java Code:
    if (char == 'a' ) {
        do this
    } else if (char == 'b') {
        do that
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Using an existing char variables stored value to call a method on a vector?

    use the value of a char to perform an operation on a Vector which has the same name as the char's value.
    Variable names are assigned when you type in the code. No way to generate one when executing.
    To use the value of a variable to find an object, you can use if/else as Junky suggested or use a Map that uses the char's value as the key to get the Vector object.
    Or if the char values are contiguous say a-z only then use a 26 element array of Vectors and use the chars value -'a' to index the array. Note you can do arithmetic with chars: 'b' - 'a' = 1
    Tolls likes this.

Similar Threads

  1. Replies: 0
    Last Post: 08-04-2011, 05:16 PM
  2. java call stored procedure
    By namalsera in forum JDBC
    Replies: 1
    Last Post: 01-04-2011, 03:53 PM
  3. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  4. How to call Oracle stored procedure from Java?
    By parag_narkhede in forum New To Java
    Replies: 4
    Last Post: 10-07-2009, 07:50 AM
  5. Vector getting re-created with each call
    By nidhirastogi in forum Advanced Java
    Replies: 6
    Last Post: 10-17-2008, 07:37 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
  •