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]);
}
}
}
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.
Re: max length of an array?
sorry, does
Code :
ArrayList<Integer> numbers = new ArrayList<Integer>();
replace
Code :
int frequency [] = new int[12];
?
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)