How do I return an array from a method?
Posted
by
dwwilson66
on Stack Overflow
See other posts from Stack Overflow
or by dwwilson66
Published on 2012-03-19T23:22:24Z
Indexed on
2012/03/19
23:29 UTC
Read the original article
Hit count: 268
I'm trying to create a deck of cards for my homework. Code is posted below. I need to create
four sets of cards (the four suits) and am create a multidimensional array. When I print the results instead of trying to pass the array, I can see that the data in the array is as expected. However, when I try to pass the array card
, I get an error cannot find symbol
. I've got this modeled after texbook and Java tutorial examples, and I need some help figuring out what I'm missing. I've over-documented to give an idea of how I'm thinking this SHOULD work...please let me know where I've gone horribly wrong in my understanding.
import java.util.*;
import java.lang.*;
//
public class CardGame
{
public static int[][] main(String[] args)
{
int[][] startDeck = deckOfCards(); /* cast new deck as int[][], calling method deckOfCards
System.out.println(" /// from array: " + Arrays.deepToString(startDeck));
}
public static int[][] deckOfCards() /* method to return a multi-dimensional array */
{
int rank;
int suit;
for(rank=1;rank<14;rank++) /* cards 1 - 13 .... */
{
for(suit=1;suit<5;suit++) /* suits 1 - 4 .... */
{
int[][] card = new int[][] /* define a new card... */
{
{rank,suit} /* with rank/suit from for... loops */
};
System.out.println(" /// from array: " + Arrays.deepToString(card));
}
}
return card; /* Error: cannot find symbol
}
}
© Stack Overflow or respective owner