java - Wondering what should I do if the length of an array is even -


i have written code find middle element sorted array. here part of code:

java code: int[] = {1,2,3,4,5}; int x = a.length;         if (x % 2 != 0){             int mid = 0 + x/2;             system.out.println("the middle element : " + a[mid]);         } 

i'm getting output wondering should write in else condition of code above? in other words, should find middle element if a.length even? possible?

we can't answer you. depends on requirements. here options:

  • return 2 middle ones.
  • return earlier option that's in middle.
  • throw exception method can't called on length.
  • etc.

Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -