ArrayIndexOutOfBounds on enhanced for loop

前端 未结 2 728
囚心锁ツ
囚心锁ツ 2021-01-28 03:59

I am trying to figure out part of an assignment and I have been beating my head against a wall for some time now. I\'m trying to transcribe DNA sequences to RNA sequences. I am,

相关标签:
2条回答
  • 2021-01-28 04:36

    You want 1 less than size , so : if (pos1 >= linkedList.size()) {.

    When pos1 == linkedList.size() it will be out of bounds

    0 讨论(0)
  • 2021-01-28 04:37

    The problem is in the statement arr[a] ='U';

    The problem is that char is represented as an int internally and 'T' equals 84 hence you get an ArrayIndexOutOfBoundsException. You need to iterate over it with a traditional counter:

    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == 'T') {
            arr[i] ='U';
        }
    }   
    
    0 讨论(0)
提交回复
热议问题