简体   繁体   中英

How do I return an array from a method?

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

card only exists within the for loop: variables are only valid within the block ( {..} ) within which they are declared.

Note rules for the main() method (from section 12.1.4 Invoke Test.main of JLS 3.0 ):

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.

Ahh... Scope is your issue... Look where card is declared relative to where it is returned from. Remember when you declare a variable within a loop or sub function its scope is limited to that location on the stack.. If this doesn't help message me back.

The reference card is limited to the scope of your inner for loop. A variable only exists inside of the inner most set of braces {} that enclose it.

You want to make one array, outside the loop, and fill it up in the loop. Not make a totally new array for each pass of the loop.

Your card was defined in the for loop. Try with it defined at the top, otherwise it gets redefined every loop, and your function will return null.

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; 
        int[][] card = new int[][]    /* define a new card...  */
        for(rank=1;rank<14;rank++)    /* cards 1 - 13 .... */
        {
            for(suit=1;suit<5;suit++)  /* suits 1 - 4 .... */
            {

                {
                    {rank,suit}      /* with rank/suit from for... loops */
                };
                System.out.println(" /// from array: " + Arrays.deepToString(card));
             }
         }
         return card;  /*  Error: cannot find symbol 
    }
}

Thanks to everyone who pointed out scope as my issue...that was definitely it. Here's my revised code, which I'm happy to report works fantastically.

import java.util.*;
import java.lang.*;
//
public class CardGame 
{
    static int card[][];   /* card is now a global variable...and static so it can */
                           /* interact with static methods.  */
//
    public static void main(String[] args)
    {
        int card[][];
        int[][] startDeck = deckOfCards();
        System.out.println(" /// from array: " + Arrays.deepToString(startDeck));
    }


    public static int[][] deckOfCards()
    {
        int rank;
        int suit;
        for(rank=1;rank<14;rank++)
        {
            for(suit=1;suit<5;suit++)
            {
                card = new int[][]
                {
                    {rank,suit}
                };
                System.out.println(" /// from array: " + Arrays.deepToString(card));
             }
         }
         return card;
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM