Java list index out of range

In java, what is the best way to switch characters in a string while keeping the rest of the characters in their original spot. My real concern is if there's a way to deal with this index out of range error other than the following: Let's say you want to switch the letter r with the capital E in "stringExample".

you can chunk this into "st" + "r" + "ing" + "E" + "xample"

---------------------------->(0,i)--(i,i+1)--(i+1,k)--(k,k+1)---(k+1)

then to switch "r" & "E": "st" + "E" + "ing" + "r" + "xample"

----------------------------->(0,i)--(k,k+1)--(i+1,k)--(i,i+1)---(k+1)

The only problem is if k is the last index then you need to write an if statement to catch this exception. Other than doing what I suggested before, is there a better way of switching these letters?

java,indexing,range,

1

Ответов: 0

Java list index out of range

In java, what is the best way to switch characters in a string while keeping the rest of the characters in their original spot. My real concern is if there's a way to deal with this index out of range error other than the following: Let's say you want to switch the letter r with the capital E in "stringExample".

you can chunk this into "st" + "r" + "ing" + "E" + "xample"

---------------------------->(0,i)--(i,i+1)--(i+1,k)--(k,k+1)---(k+1)

then to switch "r" & "E": "st" + "E" + "ing" + "r" + "xample"

----------------------------->(0,i)--(k,k+1)--(i+1,k)--(i,i+1)---(k+1)

The only problem is if k is the last index then you need to write an if statement to catch this exception. Other than doing what I suggested before, is there a better way of switching these letters?

01java,indexing,range,
Похожие вопросы