0

I cannot seem to understand the concept of variable length argument lists. When I do a bit of research, it makes sense, but I can't figure out this question to save my life.

What is the result of the following call?

get(1, 2, 3, 4, 5, 6, 7); // The Call

public int get(int ... a) {
    return a[2];
}             

My answer was "2" which is the only thing that makes sense to me. The other options were 1, 3, or 4. Thanks for your time.

xtratic
  • 4,600
  • 2
  • 14
  • 32
Taylor
  • 15
  • 5
  • 4
    It's just like a regular array. It returns the value at the index `2`, which would be `3`. Remember that arrays are zero-based. – Logan Apr 30 '18 at 19:53
  • Why don't you test it? https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – JB Nizet Apr 30 '18 at 19:53
  • Arrays are zero based. Which is to say the first item is at position 0, the second at position 1. So the item at position 2 in your case is the integer 3 – bhspencer Apr 30 '18 at 19:54
  • Thanks for the feedback everyone! I find programming to be akin to math; I'm always forgetting at least one simple detail/rule. – Taylor Apr 30 '18 at 20:06

1 Answers1

1

Arrays are zero based. Which is to say the first item is at position 0, the second at position 1. So the item at position 2 in your case is the integer 3.

bhspencer
  • 13,086
  • 5
  • 35
  • 44