I wrote a method to insert a character in an string array periodically. I wrote it in android studio, and the application stops running. My input is an array of string: (The part of input is just for test, and I will make it dynamic)
String[] myPoints= new String[4];
myPoints[0]="22";
myPoints[1]="45";
myPoints[2]="34";
myPoints[3]="60";
Now I need to insert ", " between each two records of array. So I wrote this:
public static String[] insertPeriodically(String[] points){
int pointsSize = points.length;// length of points array
int withCommosSize=pointsSize+(pointsSize/2)-1;//length of new array
String[] withCommos= new String[withCommosSize];
int insert=0;
int k=0;
for (int i=0; i<pointsSize; i=i++){
withCommos[k]=points[i];
insert=insert+1;
if(insert==2){
withCommos[k+1]=",";
k++;
insert=0;
}else
k++;
}
return withCommos;
}
The console shows this java error: java.lang.RuntimeException: Unable to start activity ComponentInfojava.lang.ArrayIndexOutOfBoundsException: length=5; index=5
Do you have any suggestion where is the problem?