views:

77

answers:

3
+1  Q: 

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? :\

+7  A: 

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.

David Zaslavsky
+2  A: 

That's not an error. The JVM simply prints the address of the array since it doesn't print its content. Try this and see what happens now?

System.out.println(getItem(1)[0]);
Am
A: 

On Object.toString()

The reason why you're getting such string is because arrays simply inherit and not @Override the Object.toString() method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character @, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

To return a String representation of an array that lists its elements, you can use e.g. Arrays.toString, and for "multidimensional" arrays Arrays.deepToString

Related questions

On deepEquals and deepToString for "multidimensional" arrays:


On defining your own type

It needs to be said that your usage of String[] is not the best design choice.

Things would be so much better had you defined your own class BasicItem supported by various enum, with as many final fields as is practical to enforce immutability; perhaps something like this:

public enum ItemType {
   KNIFE, SWORD, AXE;
}
public enum Attribute {
   SELLABLE, EDIBLE;
}

public class BasicItem {

   final String name;
   final String desc;
   final ItemType type;

   final int attackAdd;
   final int defenseAdd;

   final Set<Attribute> attributes;
   //...
}

You should really take advantage all the benefits of good object-oriented design.

See also

polygenelubricants