I need to dynamically resize an array several times rather than guessing how many elements will be in it. I have code done for this but it doesn't seem to be working, Could anyone help me figure out what's wrong! Basically I need to keep adding to the match array when a match is found (another method is implemented for this).
Currently it just fills the matches array and then gives an ArrayIndexOutOfBoundsException for the next element it tries to put in the array.
Here are the 2 functions.
Thanks
private static String[] subStrings(String[] tokens) {
String[] matches;
matches = new String[40]; //creates a new array of matches
for (int i = 0; i <=tokens.length; i++){
for (int j = i+1; j <tokens.length;j++){
if(Text.match(tokens[i],tokens[j])){
matches[i]=(tokens[i]+" | "+tokens[j]);
System.out.println(matches[i]);
if(matches[matches.length-1]!=null){
reSize(matches, matches.length+10);
}
}
}
}
public static String[] reSize(String[] matches, int s){
if(s<0){
return null;
}
String BiggerMatch[] = new String[s];
for(int i=0; i< matches.length; ++i){
BiggerMatch[i]=matches[i]; //saves the original array in a temporary variable
}
matches = new String[s]; //adds s integer to the array size of matches
for(int i=0; i<=matches.length - s ; i++){ //leaves s spaces null at the end of the array
matches[i]= BiggerMatch[i];
}
matches = BiggerMatch;
subStrings(matches); //sending the new array back to the subStrings method
return BiggerMatch;//returns the new array
}
}