7

I have this loop here

 for(int i =0; i < prices.length; i++)
  {
        if(prices[i]>largest)
        {
            largest = prices[i];
        }

        else if(prices[i]<smallest)
        {
            smallest= prices[i];
        }
  }

which loops through the whole array and finds the min and max value. Say I wanted to only loop through the first 20 elements how do I do that? I have tried along the lines of putting a nested loop in under this for loop and seeing if I come across it but I can't.

3
  • 10
    Change the condition to i < prices.length && i < 20 Commented Feb 4, 2013 at 22:09
  • 13
    No offense, but may be it is better to first read literature about basics of the language? Commented Feb 4, 2013 at 22:10
  • 3
    most intro textbooks don't show that you can put any boolean expression in the loop clause. be nice and help OP out Commented Jun 17, 2014 at 16:44

7 Answers 7

22

You could just add the requirement to the loop control condition:

for(int i =0; i < prices.length && i < 20; i++)

This would check the first 20 elements where ther are more than 20 in the array, but the whole array if there are less than 20 items.

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

1 Comment

Thank you! The problem I was having, I though about user2033853 but that didnt loop through that array! Thank you for your kind help!
6
for(int i =0; i < 20 && i < prices.length; i++)

This will loop through 20 times, i.e. first twenty elements of the array.

2 Comments

And what about when there are less than 20 elements in prices?
@nkr I edited the answer to check the length of the array as well.
6

5 answers and they all have a double comparison in the loop?

No wonder, Java programs run so slowly...

The correct way to do such a loop is:

 for(int i = 0, len = Math.min(prices.length, 20); i < len; i++)

moving the comparison between length and 20 out of the loop and evaluating the loop-condition therefore twice as fast. (ignoring what the JIT might or might not be doing)

Also, you have to initialize largest/smallest with the first element (or you get invalid values, if there is only one element in the array due to the else), and then you can skip the first element in the loop, making it even "faster":

 largest = prices[0];
 smallest = prices[0];
 for(int i = 1, len = Math.min(prices.length, 20); i < len; i++)

5 Comments

did you see my answer before posting yours? i did exactly what you did.
@foampile: Yes, but you also call Min in the loop, instead before the loop
i think the java optimizer will take care of redundant evaluations
here, i was not 100% sure so i asked the question: stackoverflow.com/questions/14711125/…
Not only will the compiler likely hoist the constant, but also any program written by a novice who'd ask this question is likely to have much larger inefficiencies than this. Encouraging them to think about such tiny microoptimizations is not going to help them at all.
5

Replace prices.length with Math.min(20, prices.length), which is the length of the array or 20, whichever is smaller:

for(int i =0; i < Math.min(20, prices.length); i++)

Comments

4

Change your for loop to something like this:

for(int i =0; i < (prices.length < 20 ? prices.length : 20); i++)
{
    if(prices[i]>largest)
    {
        largest = prices[i];
    }
    else if(prices[i]<smallest)
    {
        smallest= prices[i];
    }
}

Comments

3

If you only want to loop through the first 20 elements, then say so in the header of the for loop, like this.

for(int i =0; i < prices.length && i < 20; i++)

1 Comment

@AndrewCooper I edited it a while ago, make sure you reload the question before you downvote.
-1

maximum value is correct to get minimum is simple make max = min then work like then

if(min>x[i])
  min=x[i];

1 Comment

Why randomly answer a question from 3 years ago?

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.