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?

Recommended Answers

All 4 Replies

use println statements to check the values of max1 to 3 at the loop

commented: bad comment -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 :(

commented: good comment +11

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.

Member Avatar for hfx642

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.