Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Beginner question about Java array


  • Please log in to reply
1 reply to this topic
burton666
  • Members
  • 186 posts
  • Last active: Jul 05 2017 05:35 AM
  • Joined: 10 Aug 2012

Hi I have just started to read up on java and have tried to construct some exampel tasks. But I am struggeling with this one.

 

Find the SECOND biggest number in an array.

 

I understand how to find the smallest number like this:

int smallest = ar[0];

for(i=0; i < ar.lenght; i++){
if (ar[i] < smallest){
smallest=ar[i]
}
}

But how do I find the second smallest number?



FischBrei
  • Members
  • 2 posts
  • Last active: Mar 13 2014 08:25 PM
  • Joined: 10 Mar 2014
		int smallest = ar[0];
		int small = ar[0];

		for(int i=0; i < ar.length; i++ )
			if (ar[i] < smallest)
				smallest = ar[i];

		for(int i=0; i < ar.length; i++ )
			if (ar[i] < small && ar[i] > smallest )
				small = ar[i];