-1

I want to splice an array in Java. I tried the method Array.copyOfString to solve the problem. However, the output in the terminal is just two strange strings. Is there anything I can do to fix the functioning of the code?

package exp;

import java.util.Arrays;

public class exp {
  public static void main(String[] args){
     int[] array={0,1,2,3,4,5,6,7,8,9,10};
     int[] a=Arrays.copyOfRange(array, 0, 10);
     int[] b=Arrays.copyOfRange(array, 5, 5);
     System.out.println(a);
     System.out.println(b);
  }
}

terminal:

[I@6acbcfc0
[I@5f184fc6

Process finished with exit code 0
Shawn Xu
  • 67
  • 6

2 Answers2

1

When we pass parameter to System.out.println, it internally calls the toString method for that object (of the passed internal parameter).

Here in your case when you pass an array object to print, it calls the default toString method of Object class (super class of all objects).

And default implementation of toString in Object class is:

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

And this what you are seeing in result. So if you want to print the internal values of array you need to use Arrays.toSTring method as below :

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

It internally iterate over each element of array and return concatenated string as result to display.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
0

Try this:

System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
HassanYu
  • 55
  • 3
  • 11