5 Replies Latest reply: Dec 7, 2006 3:02 PM by 807607 RSS

    extracting the name of a passed array

    807607
      is it at all possible to extract the name of an array that is being 'passed' by a method?

      examaple:

      private void somemethod (String[] a)
      {
      ......
      }

      what i want to do is compare this array name with my instance variables (which are arrays as well) through a switch statement, and depending which array it is, perform whatever the method is meant do to.

      thanks
        • 1. Re: extracting the name of a passed array
          807607
          No.

          But the fact that you want to concerns me about what you are trying to do. Sounds like you should be using some OO but are trying to use a switch instead.
          • 2. Re: extracting the name of a passed array
            807607
            is it at all possible to extract the name of an array
            that is being 'passed' by a method?

            examaple:

            private void somemethod (String[] a)
            {
            ......

            what i want to do is compare this array name with my
            instance variables (which are arrays as well) through
            a switch statement, and depending which array it is,
            perform whatever the method is meant do to.

            thanks
            You should never need to do anything like that. Sounds like you have a design issue.
            • 3. Re: extracting the name of a passed array
              807607
              yeah, I just realize I can't do it simply because i'd have to pass different parameters depending the case. I was simply trying to shorten my code as I have three methods doing very similar tasks depending which array i'm passing through (creating the card game 'WAR' hehe).

              but thank you for your concern :)
              • 4. Re: extracting the name of a passed array
                807607
                Well you shouldn't need to duplicate code either. What do your methods have to do?
                • 5. Re: extracting the name of a passed array
                  807607
                  //add the cards to the Win Pile of the player who had the highest card when they have been compared
                  
                            private void winPile(String[] a, String x, String y)
                            {
                                 int scan = 0; 
                                 while((scan < a.length) && (a[scan] != null)) 
                                      scan++;
                                 if (a[scan] == null)
                                 {
                                      a[scan] = x;
                                      a[scan+1] = y;
                                 }
                            }
                            
                  //add the cards to the War Pile if both cards have equivalent value (suits are not taken into account)
                  
                            private void warPile(String[] a, String x)
                            {
                                 int scan = 0; 
                                 while((scan < a.length) && (a[scan] != null)) 
                                      scan++;
                                 if (a[scan] == null)
                                 {
                                      a[scan] = x;
                                 }
                            }
                  so both the methods are part of this next method:
                  //comparing player Hand decks
                  
                  private void compareDeck(String[] a, String[] b, int i, int round, boolean war)
                  {
                       String card1 = a;
                       String card2 = b[i];
                       if (cardValue(card1) == cardValue(card2))
                       {
                            war = !war;
                            warPile(this.warPile1, card1);               warPile(this.warPile2, card2);
                            System.out.println("Round: " round '\t'+ " Player1: " card1
                                      '\t'+ " Player2: " card2 '\t'+ " Result: War is on baby! Woohoo!!");
                       }
                       else
                       {
                            if (cardValue(card1) > cardValue(card2))
                            {
                                 winPile(this.winPile1, card1, card2);
                                 if (war == true)
                                 {
                                      winWar(this.winPile1, this.warPile1, this.warPile2, card1, card2 );
                                 }
                                 System.out.println("Round: " round '\t'+ " Player1: " card1
                                           '\t'+ " Player2: " card2 '\t'+ " Result: Player 1 wins");
                            }
                            else
                            {     
                                 winPile(this.winPile2, card2, card1);
                                 System.out.println("Round: " round '\t'+ " Player1: " card1
                                           '\t'+ " Player2: " card2 '\t'+ " Result: Player 2 wins");
                            }
                       }                    
                  }


                  now i've just started working on method winWar which if there was a war in the previous round, the next round determines who wins if the cards aren't of equal value. So it performs a similar task, but with different parameters. So that's why I tought there was a way to 'join' all three methods into one hehe.

                  Message was edited by:
                  aauer