Question: How many maximum words can i store in String array in java? I am working on machine learning algorithms and my requirement is huge approximately 3000 words. Suggest me any alternative to process that data because i have tried with array and it is not working.

Answer: 
You have stated you are recieving an ArrayIndexOutOfBounds exception, this is because you are using more than the stated size of the array

```
String[] strings=new String[3000];
strings[3000]="something";//causes exception because strings[2999] is the last entry.

```

If you know how many entries you require then declare an array of that size, or if you need an array style container that can be extended use an arraylist.

```
ArrayList<String> strings=new ArrayList<String>();
strings.add("Something"); //can be added as many times as you want (or that available memory will allow)

```

ArrayLists automatically resize as you add items to them, they are ideal when you want list behaviour (i.e. things are in an order) but don't know in advance how many items you will have.

You can then retrieve items from the list as you see fit, most common methods are;

```
String string=strings.get(0); //returns the first entry
int size=strings.size(); //tells you how many items are currently in the array list

```

### Notes

You can improve ArrayList performance by telling it how big you expect it to be, so `ArrayList<String> strings=new ArrayList<String>(3000);` but this is entirely optional
You can find how much memory you have for your disposal at JVM by using this code :

```
long maxBytes = Runtime.getRuntime().maxMemory();
System.out.println("Max memory: " + maxBytes / 1024 / 1024 + "M");

```

Note that if you want to know how much strings in array you can have, devide the whole number by ~64, which is average length of String. (counting all the references etc.)

```
System.out.println("Max words: " + maxBytes / 64 + " words");

```

If you have average machine, you should have at least 2GB RAM at your disposal just for allocating variables, which is ~30 milions average words.