Search code examples
javaarraysoperators

Java: Array and using ++ operator when adding values


How exactly does the ++ operator work when added to a normal array of ints like this myArray[range]++;

if I got a selection of values (range) being added iteratively 1,2,3,3,3,4,4 will it add 1 once, 2 once, 3 three times and 4 two times? And does it just add it to the end of the array?


Solution

  • myArray[range]++; will increment the value at index range. If you want to increment all values do -

     for (int i = 0; i < MyArray.Length; i++)
          MyArray[i]++;