0

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
}

}

  • 1
    Why don't you use an ArrayList? It is a dynamic data structure so you do not need to boher with resizing. – Dimitris Fousteris Oct 26 '14 at 19:33
  • You can't (even if you can fake it) resize an array. That's not what they're for. Arrays are used for __fixed__ amounts of data. It sounds like you might want an `ArrayList` instead. – BitNinja Oct 26 '14 at 19:33
  • The paramerers I need to follow are // Instead I also want you to write (and use) a method that resizes the array whenever necessary. // However, you know that arrays cannot be resized. // That means your method is really going to copy the values from a small array into a large array // and return the big array – Matthew Finn Oct 26 '14 at 19:35
  • I also don't know how to continue using the new array in the subStrings method then. – Matthew Finn Oct 26 '14 at 19:37

2 Answers2

0

Use an ArrayList. ArrayLists are Lists with a backing array of the same type.

ArrayLists follow a certain resize strategy(See also here: ArrayList: how does the size increase?). So if the elements exceed the backing arrays size a new array will be created and elements in the "old" array will be copied over.

If you really need to have an Array as return value you can simply use the toArray method of the List:

 ArrayList<String> matches = new ArrayList<String>();
 ....
 for(....) {
     matches.add(someString);
 }
 ....
 return matches.toArray(new String[matches.size()]);
Community
  • 1
  • 1
fyr
  • 20,227
  • 7
  • 37
  • 53
0
public String[] resize(String[] original, int extra) {
   // You are right you can't resize an array,
   // But we can make a new one with extra amount of indexes
   String[] newArray = new String[original.length + extra];
   // Then we need to copy the original memory over to the new
   // array. This leaves the end of the array all null.
   System.arrayCopy(original, 0, newArray, 0, original.length);
   // Then return it
   return newArray;
}

Now while using this you have to do the following in your calling code,

/// ....
if (matches[matches.length-1] != null) {
   matches = resize(matches, 10);
}

This is because like you stated you cannot really resize an array. So you need to replace your array in this stack context, with the array created by the resize method.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64