# Array passing variables

• (2 Pages)
• 1
• 2

## 17 Replies - 672 Views - Last Post: 09 October 2012 - 01:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294917&amp;s=89666d250eed52615e6b96894528fef3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 baavgai

• Dreaming Coder

Reputation: 6378
• Posts: 13,626
• Joined: 16-October 07

## Re: Array passing variables

Posted 09 October 2012 - 01:13 PM

The problem is now the size of the hand...

I'd go the other way:
```final int HAND_SIZE = 13; // magic numbers, bad
int[] handOne = new int[HAND_SIZE];
int[] handTwo = new int[HAND_SIZE];
//...
int cardNo = 0;
for (int i = 0; i < HAND_SIZE; i++) {
handOne[i] = deck[cardNo++];
handTwo[i] = deck[cardNo++];

```

### #17 alex71385

Reputation: 0
• Posts: 74
• Joined: 27-October 09

## Re: Array passing variables

Posted 09 October 2012 - 01:44 PM

g00se, on 09 October 2012 - 02:01 PM, said:

Quote

Is there a way to do different single arrays?

Certainly. Two ways:

a. Get rid of the loop and just do the arraycopy four times
b. Use a single loop and adjust the indexes appropriately

Thank you!
```public static void deal(int[] deck) {
int[] handOne = new int[13];
int[] handTwo = new int[13];
int[] handThree = new int[13];
int[] handFour = new int[13];

shuffle(deck);

System.arraycopy(deck, 0, handOne, 0, 13);
System.arraycopy(deck, 13, handTwo, 0, 13);
System.arraycopy(deck, 26, handThree, 0, 13);
System.arraycopy(deck, 39, handFour, 0, 13);

for (int i = 0; i < 13; i++) {
System.out.print(handOne[i] + " " );
System.out.print(handTwo[i] + " " );
System.out.print(handThree[i] + " " );
System.out.print(handFour[i] + " " );
//String suit = suits[deck[i] / 13];
//String rank = ranks[deck[i] % 13];

}
}

```

I got it with the arraycopy. Now I'm trying to implement the assignment of suit and rank to the card that was originally done with the deck array (as seen with // here) to each one of the hand arrays. I know the best way would be creating it's own method and just passing different array to it each time, but I've only done methods with parameters already assigned. Not sure how I can write out the code and pass array to it later instead of writing
```public static void cards(int[] handOne) {
String suit = suits[handOne[i] / 13];
String rank = ranks[handOne[i] % 13];
}

```

Because in the end I need to print out something like: "Hand 1: 1A 5S JA etc" for each array.

### #18 g00se

• D.I.C Lover

Reputation: 3102
• Posts: 13,374
• Joined: 20-September 08

## Re: Array passing variables

Posted 09 October 2012 - 01:58 PM

Quote

I got it with the arraycopy.

Yes, but you should avoid 'magic numbers'. Use a variable to specify index. pbl's method is another approach worth looking at.

baavgai's comment should also be re-examined. You're now beginning to use 'parallel arrays' which should be avoided