Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java return arrays

I'm having trouble returning arrays from a custom method. It compiles fine but I get back:

[Ljava.lang.String;@20cf2c80
Press any key to continue . . .

I use:

System.out.println(getItem(1));

code:

public static String[] getItem(int e) {

String[] stats = new String[7];

            String name = "Null";
            String desc = "None";
            String typeOf = "0";
            String attackAdd = "0";
            String defenseAdd = "0";
            String canSell = "true";
            String canEat = "false";
            String earnedCoins = "0";



            if (e == 1) {

        name = "Pickaxe";
        desc = "Can be used to mine with.";
        typeOf = "2";
        }

      return new String[] { name, desc, typeOf};

    }

Help? :\

like image 978
test Avatar asked May 04 '26 08:05

test


1 Answers

The toString() method of an array object actually doesn't go through and produce a string representation of the contents of the array, which is what I think you wanted to do. For that you'll need Arrays.toString().

System.out.println(Arrays.toString(getItem(1)));

The notation [Ljava.lang.String is Java code for a String array - in general, the default string representation of an array is [L followed by the type of the array's elements. Then you get a semicolon and the memory address (or some sort of locally unique ID) of the array.

like image 193
David Z Avatar answered May 06 '26 21:05

David Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!