0

I would like to know why and how to fix this array method outputs the memory representation of temp.

public class StringMethodsExersice{
    public static int[] shiftMax(int[] num){
        int temp[] = new int[num.length];
        int firstEl = num[0];
        int lastEl = num[num.length-1];


        for(int i=1;i<num.length-1;i++){
            if(num[i] > num[i+1]){
                temp[i] = num[i];
            }
            temp[i] = num[i];
        }
        temp[0] = firstEl;
        temp[num.length-1]= lastEl;
        return temp;
    }

    public static void main (String []args){
        int[] myArray = new int [5];
        myArray [0] = 2;
        myArray [1] = 5;
        myArray [2] = 6;
        myArray [3] = 14;
        myArray [4] = 25;
        System.out.println(shiftMax(myArray));
    }
}

1 Answer 1

5

You can use Arrays#toString:

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

(See its implementation for details)


Explanation about the output you're getting:

Since each object has toString() method, the default is displaying the class name representation, then adding @ sign and then the hashcode.

Sign up to request clarification or add additional context in comments.

2 Comments

Stop stealing my answers! :P +1
Also note the existence of Arrays.deepToString() for embedded arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.