Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)
edited by

This is code I have written on Eclipse IDE. But I get garbage output. 

public class RemoveTest {

public static void main(String[] args) {

    // TODO Auto-generated method stub

System.out.print(remove(new int[] {1, 2, 3, 4}));

}

static int [] remove(int[] a){

    int[] b = new int[a.length - 1];

    System.arraycopy(a, 1, b, 0, a.length - 1);

    return b;

}

}

Can anyone tell me how I should fix this code to get the desired output? 

1 Answer

0 votes
by (19.7k points)

You are getting the results of the Object's toString() method. Since arrays are objects too, use  Arrays.toString():

System.out.print(Arrays.toString(remove(new int[] {1, 2, 3, 4})));

If you want to learn more about Java? Check this out: Java course on Intellipaat   

Browse Categories

...