0
public static Integer[] getTopThree(Integer[] values)
	{
		int max1 = Integer.MIN_VALUE;
		int max2 = Integer.MIN_VALUE;
		int max3 = Integer.MIN_VALUE;  //assuming integer elements in the array

		for (int i = 0; i < values.length; i++)
		{
		    if (values[i] > max1)
		    {
		        max3 = max2; max2 = max1; max1 = values[i];
		    }
		    else if (values[i] > max2)
		    {
		        max3 = max2; max2 = values[i];
		    }
		    else if (values[i] > max3)
		    {
		        max3 = values[i];
		    }
		}
	
		Integer[] topThree = {max1, max2, max3};
		return topThree;
	}

It is returning this:
[Ljava.lang.Integer;@addbf1
[Ljava.lang.Integer;@addbf1
[Ljava.lang.Integer;@addbf1

What am I doing wrong?

4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by hfx642
1

@OP
Then how can you check if the values of max1, max2 to max3 at the loop is satisfactory and know where the problem in assigning might lay considering that the function is returning garbage values?
You did not even post the values your trying to pass to getTopThree() so its more on guesswork on our side

I'm just trying to help here :(

Edited by zeroliken: n/a

Comments
good comment
0

It is returning this:
[Ljava.lang.Integer;@addbf1

That printout is from an array of Integer object's toString method. It returns the datatype and the object's hashcode. If you want to see the contents of the array, use the Arrays toString() method to format it for printing.
To get a single element use array notation([]) with an index.

Edited by NormR1: n/a

0

How are you checking to see what is being returned?
Your method MAY be operating correctly.
It is how you are checking the results, that are at fault.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.