I need a little help. I’m trying to put together a small Java program that creates and returns an array containing all elements of a, each of these duplicated. However, every time I try to execute my code I get an “out of bounds” exception. Please give me an idea of where I’m going wrong. Thanks! Here is what I have so far:
public class integerStutter
{
public static int[] stutter(int [] a){
int[] j = new int[a.length*2];
for(int i=0; i < j.length; i++){
j[i] = a[i];
j[i+1] = a[i];
i++;
}
return j;
}
}
The old and new array are not of equal sizes. You are trying to access elements from old array using valid indices for the new array (which is of double size) and this is causing the exception.
Instead try: