5

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?

1
  • There is no adding an element to a Java array. Commented Sep 2, 2022 at 13:27

5 Answers 5

7

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]++; 
Sign up to request clarification or add additional context in comments.

Comments

5

int myArray[range]++; Will increment 1 to the element in the position range.

To increment all the element in the array you just have to do:

for(i = 0; i < myArray.length; i++) myArray[i]++; 

Comments

2

myArray[range]++

This would simply increment the value at the index range in the int array myArray.

Comments

1

myArray[range]++ will add the value at the index like the other ppl mentioned. in your example, you will get:

2,3,4,4,4,5,5

if you used range as the "iterator"

Comments

-1

equivalent to

myArray[range] = myArray[range] + 1; 

2 Comments

What about value = myArray[range]++ vs. value = (myArray[range] = myArray[range] + 1) (and value = (myArray[range] += 1)?
@greybeard it will be better if you explain this in separate answer then just mentioning it in comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.