Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: max length of an array?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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:
    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.
    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


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: max length of an array?

    Not sure if i need to, but here is all my code so far

    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]);
    			}
    	}
    }

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    // 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,
    		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.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: max length of an array?

    sorry, does
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    replace
    int frequency [] = new int[12];
    ?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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)

Similar Threads

  1. Getting length of individual token?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 10th, 2011, 02:48 AM
  2. Finding the length of a two dimensional array
    By petemyster in forum Java Theory & Questions
    Replies: 2
    Last Post: December 12th, 2010, 10:21 PM
  3. text.length method problem?
    By computercoder in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2010, 10:40 AM
  4. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM
  5. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 07:24 AM