Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you declare a string array of unknown size? [duplicate]

Possible Duplicate:
Declaring an array of unknown size

I'm working in Java and I am trying to input a sentence into a string array. I am tokenizing it and determining the word count. However, I need to add each word into a string array in order to determine if there are duplicates or not. I am not sure how to initialize my array if I don't know the word count until later in the program.

  //Declares variables
  Scanner scan = new Scanner (System.in);
  int withoutdup = 0, wordCount = 0;
  String line, word; 
  StringTokenizer tokenizer;
  List<String> sentence = ArrayList<String>;

  //Asks user for input
  System.out.println ("Please enter text. Enter DONE to finish.");
  line = scan.next();


  //Tokenizes the string and counts the number of character and words
while (!line.equals("DONE"))
 {
     tokenizer = new StringTokenizer (line);
     while (tokenizer.hasMoreTokens())
     {
        word = tokenizer.nextToken();
        wordCount++;
        sentence += word; 
     }
     line = scan.next();
 }
like image 727
user1755178 Avatar asked Oct 18 '12 05:10

user1755178


People also ask

How do you declare an array as unknown size?

int[] list = new int[5];

How do you declare a string of unknown size in Java?

There are two ways to declare string array - declaration without size and declare with size. There are two ways to initialize string array - at the time of declaration, populating values after declaration. We can do different kind of processing on string array such as iteration, sorting, searching etc.

Can you declare an array without a size?

Q #1) Can we declare an Array without size? Answer: No. It is not possible to declare an array without specifying the size. If at all you want to do that, then you can use ArrayList which is dynamic in nature.


2 Answers

Use an ArrayList instead

List<String> list = new ArrayList<String>();

it grows automatically.

To check for the duplicates, you can utilize a Set (HashSet), it doesn't allow duplicate elements.

Update

I see a couple of problem in your code:

List<String> sentence = ArrayList<String>;

You are missing the new after =.

sentence += word;

That only would work if sentence was a String. It's a List so you should use List.add method there

sentence.add(word);

Also now wordCount++; is redundant sentence.size() will tell you how many words.

like image 86
Bhesh Gurung Avatar answered Oct 20 '22 16:10

Bhesh Gurung


just see the below example, you will get the idea about how to declare a string array of unknown size.

First, use ArrayList to store the strings and each time call the .add method the ArrayList size increases by one element. When you're filling the ArrayList, use ArrayList size() method and create your String array and size it from. But make sure that each element in an ArrayList is an Object that’s why you need to convert each element to a String.

Example:

ArrayList list = new ArrayList();

for( int i = 0; i < 100; i++ )

list.add( "stuff" );

String[] strArray = new String[ list.size() ];

for( int j = 0; j < strArray.length; j++ )

strArray[ j ] = list.get( j ).toString();

Hope this will help you. It’s just one way, but I think there might be another more efficient way through you can do the same thing.

like image 42
Jessica Eldridge Avatar answered Oct 20 '22 15:10

Jessica Eldridge