ArrayIndexOutOfBounds on enhanced for loop

前端 未结 2 749
囚心锁ツ
囚心锁ツ 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: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';
        }
    }   
    

提交回复
热议问题