-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++; 
    }
user2696955
  • 33
  • 2
  • 7
  • ArrayList? http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html and http://stackoverflow.com/questions/2697182/how-to-use-an-array-list – Nico May 13 '14 at 18:50
  • The Java ArrayList grows automatically. You don't have to worry about resizing it: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html – Edwin Torres May 13 '14 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. – Bhesh Gurung May 13 '14 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. – Dawood ibn Kareem May 13 '14 at 19:09
  • thank you so much every one for the help.. – user2696955 May 14 '14 at 04:39
  • @DavidWallace : yes I am a student but not studied java as formal subject in class. learning it through tutorials videos and such forums. would you please suggest any tutorials from which I can learn java thoroughly.? – user2696955 May 14 '14 at 04:51
  • I haven't found anything better than the online Java tutorials on the Oracle site. You might also like to look for the author Bruce Eckel - you may be able to find an online copy of one of his excellent books. – Dawood ibn Kareem May 14 '14 at 04:55

4 Answers4

2

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

Kayaman
  • 72,141
  • 5
  • 83
  • 121
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.

user3633673
  • 594
  • 6
  • 6
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++;
}
Andrei
  • 3,086
  • 2
  • 19
  • 24
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).

Christian Fries
  • 16,175
  • 10
  • 56
  • 67
  • 1
    Actually `ArrayList` wins even when adding. Especially if you estimate the size properly when creating it (to minimize the resizes). – Kayaman May 13 '14 at 19:13