# max length of an array?

• March 31st, 2011, 10:08 PM
max length of an array?
I have a program that ready a file, and displays the fewuency of a length of a word, however, do to this i have used an array which specifies the max length as seen here:
Code :

`int frequency [] = new int[12];`
this makes it so that the max word possibly read is 12....

however i need this to be whatever the maximum length of a word is?
i have the following code, this kind of works, as in it displays the lengths without me telling it hte number, however it goes all the way to 115 (with length as 0 since there are no wordsth is long.
Code :

```int t = Integer.MAX_VALUE; int frequency [] = new int['t'];```

How can i fix this so that it is the max length is determind?
my idea is that i might need a for loop?

sorry, im new to Java there possibly a simple answer
• March 31st, 2011, 10:47 PM
Re: max length of an array?
Not sure if i need to, but here is all my code so far

Code Java:

```import java.io.*; import java.util.*;   class analyze1_2 { public static void main(String[] args) throws IOException { FileReader file = new FileReader("body.txt"); BufferedReader MyFile = new BufferedReader(file);   StringTokenizer TokenizeMe= new StringTokenizer(""); int NumberOfTokens = 0; int NumberOfWords = 0; int i; int t = Integer.MAX_VALUE; int frequency [] = new int[t]; String line="";   while((line=MyFile.readLine())!=null) { TokenizeMe = new StringTokenizer(line); NumberOfTokens = TokenizeMe.countTokens();   for (int WordsInLine=1; WordsInLine<=NumberOfTokens; WordsInLine++) { String word=TokenizeMe.nextToken(); int l=word.length(); frequency[l]++; }   NumberOfWords += NumberOfTokens; } MyFile.close();   for (i=1; i<frequency.length; i++) { System.out.println("Length: "+i+ ", "+ "frequency: " +frequency[i]); } } }```
• April 1st, 2011, 01:06 AM
helloworld922
Re: max length of an array?
There are two main solutions:

1. Expand your array as you go.
2. "Pre-read" to determine exactly how big your array needs to be.

The most common solution is the 1st one, using the ArrayList container. The ArrayList will automatically take care of expanding itself as needed, and all you know is what's actually valid in your list.

Code Java:

```// creates and adds some items to the ArrayList. Notice how when the ArrayList gets created, there's no indication how large it should be ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(3); numbers.add(1); numbers.add(100); numbers.add(5); // numbers now contains {3, 1, 100, 5}```

As a side note,
Quote:

Code Java:

``` int t = Integer.MAX_VALUE; int frequency [] = new int[t];```

This is a very bad idea as it will take an extremely large amount of memory (~8GB). If you want an initial buffer, pick a "reasonable size". Typically this won't go over 10000, and more often than not, probably shouldn't be over 1000.
• April 1st, 2011, 01:24 AM
Re: max length of an array?
sorry, does
Code :

`ArrayList<Integer> numbers = new ArrayList<Integer>();`
replace
Code :

`int frequency [] = new int[12];`
?
• April 3rd, 2011, 11:17 AM
helloworld922
Re: max length of an array?
Yes, in both cases you're declaring and initializing a variable. Note that when using an ArrayList, you must use the associated methods to access them (the bracket indexing method doesn't work). For the documentation for ArrayLists, see: ArrayList (Java Platform SE 6)