-3

I know this may be a simple question but so far I haven't found an answer online or on SO for it. I'm working on this school project and basically I need to read a text file and put two different kinds of words into two different arrays. The only difference is that I CAN NOT use an array list. I also need to be able to read any size file meaning that I should be able to re-size it if needed.

Therefore I need to read a file separate two kinds of words and if the file is too large I need to make the array larger. Any thoughts? I was also going to use this to separate the two kinds of words.

String[] a = line.trim().replaceAll("[^A-Z]").toLowerCase().split("\\s+");
  • 5
    You can't make an array larger, you would have to create a new array with greater size and copy over the contents of the existing array – CubeJockey Sep 04 '15 at 17:12
  • Ya I guess I worded that funky. I know that I need to make a copy of the previous array and replace it into a new one but I'm not sure how I would go about that. –  Sep 04 '15 at 17:13
  • 2
    http://stackoverflow.com/questions/13197702/resize-an-array-while-keeping-current-elements-in-java – Orch Sep 04 '15 at 17:15
  • @Orch is there a way to make the size double each time it needs to be re-sized because what if after I re-size it, it is still too small. I was thinking some form of a loop. –  Sep 04 '15 at 17:18

2 Answers2

3

You need to create a method to "stretch" your array by recreating it.

Untested Code Ahead

public String[] add(String[] arr, String el, int idx) {
  if (idx >= arr.length) { //uh-oh, out of bounds incoming
    String[] newArr = new String[arr.length * 2]; //let's grow our array by a factor of 2
    for (int i = 0; i < arr.length; i++) { //copy the old values in
      newArr[i] = arr[i];
    }
    newArr[idx] = el; //add in the new value
    return newArr; 
  }
  else {
    arr[idx] = el;
  }
  return arr;
}

And then call it like so when you want to add elements

arr = add(arr, element, i);

Note there is a bug in the above code if you attempt to add at an index that is greater than or equal to twice the current size of the array. That can be fixed with a simple sanity check early in the code. This is more to give you an idea of the logic behind amortized cost growth arrays.

Community
  • 1
  • 1
CollinD
  • 7,304
  • 2
  • 22
  • 45
  • so are you using the int idx as the length of the array? Then checking to see the size and if the size is larger than the index than the array is too small? –  Sep 04 '15 at 17:31
  • 1
    arrayCopy is certainly more appropriate. This is to show the logic, rather than a solid implementation. – CollinD Sep 04 '15 at 17:34
  • 1
    @Qix, while I agree with you, I'm going to take this as a learning opportunity. What does `arrayCopy()` do that his manual loop wouldn't? – CubeJockey Sep 04 '15 at 17:34
  • 1
    @Trobbins http://stackoverflow.com/questions/18638743/is-it-better-to-use-system-arraycopy-than-a-for-loop-for-copying-arrays Performance gains – CollinD Sep 04 '15 at 17:34
  • @JoetheDailyProgrammer idx is the index to insert the value at. So it checks if that's within bounds, and if not, grows the array. – CollinD Sep 04 '15 at 17:35
  • Beat me to it, Collin! – Qix - MONICA WAS MISTREATED Sep 04 '15 at 17:36
  • @CollinDriscoll ah ok so I was reading over your conversation and it looks like I should no use System.arrayCopy(//this is my array?) and put this in an if statement that checks the length of the file versus the length of the array? –  Sep 04 '15 at 17:41
0

is there a way to make the size double each time it needs to be re-sized because what if after I re-size it, it is still too small. I was thinking some form of a loop

Just use Arrays.copyOf(T[] original, int newLength)

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOf%28T[],%20int%29

For the newLength you can just use your current array's length * 2.

Orch
  • 887
  • 2
  • 8
  • 21
  • How do I implement this so when I do run out of space I can then copy it over and over until everything is satisfied? –  Sep 04 '15 at 17:23
  • copyOf copies the whole array into a new array of the size you specify. I'm not sure I understand what you mean by "copy it over and over..." – Orch Sep 04 '15 at 17:25
  • so if I'm reading the file and the array is to small then I use what you had said above to increase the length. What if it is still too small? How do I make it repeat this process so I can make sure that I have the right sized array? –  Sep 04 '15 at 17:26
  • I don't know how you're reading your file in, but there should be a way for you to check how big the file is (i.e. file.length) and you can use that to figure out how big your array needs to be. – Orch Sep 04 '15 at 17:35
  • but then would I ever need to resize anything if I already know the length? –  Sep 04 '15 at 17:42
  • If you know how big to make the array the first time, then you won't have to resize it. If for some reason you can't figure out the length, then you'd just have to have a loop that keeps increasing the size of the array until it fits. – Orch Sep 04 '15 at 17:47