0

So in the beginning, I just had this code to add to my array:

public void getMyArray()
{
    myArray[0] = ("String1");
    myArray[1] = ("String2");
}

But I kept getting a null pointer exception whenever I called it, and I wasn't sure why. So I changed my code to this:

public void getMyArray()
{
    String [] myArray = {"String1", "String2"};
    System.out.println(myArray);
}

And now I get what seems to be the address when printing:

[Ljava.lang.String;@1ca6218

LynnLove
  • 29
  • 2
  • 8
  • yes, it is expected behavior, what is your question? – Iłya Bursov Mar 11 '16 at 21:16
  • 2
    See Arrays.toString: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[]) – ControlAltDel Mar 11 '16 at 21:17
  • First example won't compile, because `myArray` has not been declared. If declared as a *field* (not shown), then the default value is `null`, and `NullPointerException` is an obvious result of that. – Andreas Mar 11 '16 at 21:31

5 Answers5

2

You can use Arrays.toString() like this:

System.out.println(Arrays.toString(myArray));

Replacing it in your code:

public void getMyArray()
{
    String [] myArray = {"String1", "String2"};
    System.out.println(Arrays.toString(myArray));
}

The output will be:

[String1, String2]
Atri
  • 5,511
  • 5
  • 30
  • 40
0

The toString() method for an array will not print out all objects in an array in java, unless you want to override it and make your own implementation. What is printing is a description of the array object.

To print all elements in the array you would do something like:

for(String myArray1 : myArray) {
    System.out.println(myArray1);
}

Also, the size of an array in java is fixed at instantiation. Whatever amount of memory you allocate for the array is there to stay. If you want to change the size, look into ArrayLists or LinkedLists and other structures. Hope this helps.

Preston Garno
  • 1,175
  • 10
  • 33
0

i imagine that you are not initializing youre array anywhere, using something like

myArray = new String[2];

But, besides that, the second option you have there is printing that because its actually printing the objects string encoding for a String array. instead, you will have to loop through each element and print it inidividually

for(int i = 0; i < myArray.length; i++)
{
   System.out.println(myArray[i]);
}
William B.
  • 85
  • 5
0

The reason you get [Ljava.lang.String;@1ca6218 on output is because an object's default string representation is its bytecode representation in the JVM.

Since there is no way in the Java language to override array's toString(), you can create a utility method to make a more appropriate string.

Here is an example for you:

public static String arrayToString(String[] array) {
    StringBuilder builder = new StringBuilder();
    for (String s : array) builder.append(s).append(" ");
    String result = builder.toString();
    return result.substring(0, result.length() - 1);
}

You can also use Java's built-in array to string via the Arrays utility class:

Arrays.toString(myArray)

The reason you get a null pointer or index out of bounds is because your array variable reference is either null or not to an appropriately sized array.

In your problem, you will need an array of 2 elements, thus new String[2]

You can then use normal assignment and it should work, along with the above method to print out the string.

String[] myArray = new String[2];
myArray[0] = "Hello";
myArray[1] = "there.";
System.out.println(Arrays.toString(myArray));
Jire
  • 9,680
  • 14
  • 52
  • 87
0

Use java.util.Arrays#toString

System.out.println(Arrays.toString(myArray));
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89