-1

hello I am beginner to work with java. I have following code, where I wanted to initialize the string array word[] dynamically consisting of size of total no. of tokens in all documents [] array. how should I do that?

String []result = {"Shipment of Gold damaged in fire","Delivery of silver arrived in silver truck","shipment of Gold arrived in Truck"};
String []documents = new String[result.length];
    for (int  k =0; k<result.length; ++k){ 
        documents[k] = result[k].toLowerCase();
        System.out.println("document["+k+"] :" + documents[k]);
    }
    /*step 2: Tokenize all documents  and create vocabulary from it*/
    int i=0; 
    String [] word = new String [30]; // how to do dynamic allocation here
    int no_of_tokens=0;

    for(String document:documents){
        StringTokenizer st = new StringTokenizer(document," ");
        System.out.print("tokens in document"+ i +":"+ st.countTokens()+"\n");

        while(st.hasMoreTokens()) {
            word[no_of_tokens]=st.nextToken();
            System.out.print(word[no_of_tokens] + "\n");
            no_of_tokens++;
        }
        i++; 
    }
7
  • ArrayList? docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html and stackoverflow.com/questions/2697182/how-to-use-an-array-list Commented May 13, 2014 at 18:50
  • The Java ArrayList grows automatically. You don't have to worry about resizing it: docs.oracle.com/javase/tutorial/collections/interfaces/… Commented May 13, 2014 at 18:55
  • @NicolásCarlo: This must be a homework. Usually, they come with the constraints to use such low level classes and techniques. Just compare the amount of code with the amount of work that it's doing. Commented May 13, 2014 at 18:56
  • The Javadoc for StringTokenizer says "... StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. ..." so I think you should avoid it. And if you are a student and your teacher has instructed you to use it, then show him/her the Javadoc. You should be using split for something like this, and that automatically gives you an array of the right size. Commented May 13, 2014 at 19:09
  • thank you so much every one for the help.. Commented May 14, 2014 at 4:39

4 Answers 4

2

Either use a List such as ArrayList, or use String.split() instead of StringTokenizer, it will return a String[].

Sign up to request clarification or add additional context in comments.

1 Comment

Using split() is the only reasonable answer to this question. +1 for being the only person to mention it so far.
1

I would use a java.util.ArrayList instead of a static array. You can't resize a static array, but you can create a new, bigger, static array and then copy the initial contents over.

Comments

0

You can use an implementation of the List interface, like ArrayList for this case. It will resize automatically when it almost fills so you don't have to worry about figuring the right initial size.

You use it like this:

....
/*step 2: Tokenize all documents  and create vocabulary from it*/
int i=0; 
List<String> word = new ArrayList<String>(); // how to do dynamic allocation here
int no_of_tokens=0;

....

while(st.hasMoreTokens()) {
    word.add(st.nextToken());
    System.out.print(word.get(no_of_tokens) + "\n");
    no_of_tokens++;
}

Comments

0

You may use ArrayList<String> or LinkedList<String>. The two differ in the overhead for adding elements (LinkedList is fast, ArrayList is slow) and getting elements via get(i) (LinkedList is slow, ArrayList is fast).

1 Comment

Actually ArrayList wins even when adding. Especially if you estimate the size properly when creating it (to minimize the resizes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.