extracting the name of a passed array

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

examaple:

privatevoidsomemethod (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

[423 byte] By [aauera] at [2007-10-3 11:55:15]
# 1
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.
zadoka at 2007-7-15 14:30:18 > top of Java-index,Java Essentials,Java Programming...
# 2

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

Norweeda at 2007-7-15 14:30:19 > top of Java-index,Java Essentials,Java Programming...
# 3

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 :)

aauera at 2007-7-15 14:30:19 > top of Java-index,Java Essentials,Java Programming...
# 4
Well you shouldn't need to duplicate code either. What do your methods have to do?
Norweeda at 2007-7-15 14:30:19 > top of Java-index,Java Essentials,Java Programming...
# 5

//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[i];

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

aauera at 2007-7-15 14:30:19 > top of Java-index,Java Essentials,Java Programming...