• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array and While Loop Question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok...I am having some trouble wrapping my head around the answer to a problem. Here it is:

// returns index of first occurrence of valid arr
// after position start;
// returns arr.length if val is not found
public int findNext(int[] arr, int val, int start)
{
int pos = start + 1;

while ((pos < arr.length) && (arr[pos] != val))
pos++;

return pos;
}

int[] arr = {11, 22, 100, 33, 100, 11, 44, 100};

System.out.println(findNext(arr, 100, 2));

The execution of the code results in the value 4 being printed. The way I see it, the result should be 3 right?

Also, why does the code not work if I change the condition to (arr[pos] != val) && (pos < arr.length)

Thanks for the help!!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Why do you think it should be 3?
2. You get an IndexOutOfBoundsException, right? What's the index and why is it out of bounds? Do you understand how the && operator works?
 
Brian Kenney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its 3 because of the 3 occurrences of the 100...but I am thinking the code is just counting the index locations to get 4? Based on the conditional?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming the comments are right (and that is often a dicey assumption)

// returns index of first occurrence of valid arr
// after position start;

So you are looking for the index of the first occurrence of 100 AFTER array index 2. Here is your array:


So, what is the index for the first occurrence of 100 after position 2? It looks like 4 to me.
 
Brian Kenney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh!! Thank you. I now understand.
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic