I'm trying to use this to print out only part of an array. My array is 5 elements long - {6, 4, 2, 6, 2}
- and I'd like to print just {6, 4, 2, 6, 2}
. But using my current code, it's printing out [4, 2, 6, 2]
- indexes 1 through 4, not indexes 0 through 3. Why might this be happening?
String nucList = CCATT-AATGATCA-CAGTT
int[] counter = new int[5];
for (int i = 0; i < nucList.length(); i++) {
if (nucList.charAt(i) == 'A') {
} else if (nucList.charAt(i) == 'C') {
counter[0]++;
} else if (nucList.charAt(i) == 'G') {
counter[1]++;
} else if (nucList.charAt(i) == 'T') {
counter[2]++;
} else if (nucList.charAt(i) == '-') {
counter[3]++;
}
int[] counterNucs = Arrays.copyOfRange(counter, 0, 4);
filePrint.println("Nuc. Counts: " + Arrays.toString(counterNucs));
Thanks!
EDIT: This seems to even be an issue with the arrays in my project that should be printing in full as well. Should I post more of my code?
You seem to be adjusting your counters incorrectly. For the 'A' character, you are not adjusting the counter at all. At index 0 (the first array element) you are counting the 'C' character and so on until only index 3 (the fourth element). This leaves the last element undefined.
I believe you want your code to resemble this:
String nucList = CCATT-AATGATCA-CAGTT
int[] counter = new int[5];
for (int i = 0; i < nucList.length(); i++) {
if (nucList.charAt(i) == 'A') {
counter[0]++;
} else if (nucList.charAt(i) == 'C') {
counter[1]++;
} else if (nucList.charAt(i) == 'G') {
counter[2]++;
} else if (nucList.charAt(i) == 'T') {
counter[3]++;
} else if (nucList.charAt(i) == '-') {
counter[4]++;
}
int[] counterNucs = Arrays.copyOfRange(counter, 0, 4);
filePrint.println("Nuc. Counts: " + Arrays.toString(counterNucs));