You don't need to know the array size when you declare it
String[] myArray;
but you do need to know the size when you initialize it (because Java Virtual Machine needs to reserve a continuous chunk of memory for an array upfront):
myArray = new String[256];
If you don't know what the size will need to be in the moment you initialize it, you need a List<String>, or you'll be forced to make your own implementation of it (which is almost certainly worse option).
You don't need to know the array size when you declare it
String[] myArray;
but you do need to know the size when you initialize it (because Java Virtual Machine needs to reserve a continuous chunk of memory for an array upfront):
myArray = new String[256];
If you don't know what the size will need to be in the moment you initialize it, you need a List<String>, or you'll be forced to make your own implementation of it (which is almost certainly worse option).
No, it needs to be declared, and thus have a length before you can set elements in it.
If you want to resize an array, you'll have to do something like: Expanding an Array?
How to declare string array[] of unknown size (JAVA) - Stack Overflow
java - How do you declare a string array of unknown size? - Stack Overflow
java - How do you declare a string array of unknown size? - Stack Overflow
Java: how to initialize String[]? - Stack Overflow
You don't need to know the array size when you declare it
Use with AIString[] myArray;
but you do need to know the size when you initialize it (because Java Virtual Machine needs to reserve a continuous chunk of memory for an array upfront):
Use with AImyArray = new String[256];
If you don't know what the size will need to be in the moment you initialize it, you need a List<String>, or you'll be forced to make your own implementation of it (which is almost certainly worse option).
No, it needs to be declared, and thus have a length before you can set elements in it.
If you want to resize an array, you'll have to do something like: Expanding an Array?
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.
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.
You need to initialize errorSoon, as indicated by the error message, you have only declared it.
Use with AIString[] errorSoon; // <--declared statement
String[] errorSoon = new String[100]; // <--initialized statement
You need to initialize the array so it can allocate the correct memory storage for the String elements before you can start setting the index.
If you only declare the array (as you did) there is no memory allocated for the String elements, but only a reference handle to errorSoon, and will throw an error when you try to initialize a variable at any index.
As a side note, you could also initialize the String array inside braces, { } as so,
Use with AIString[] errorSoon = {"Hello", "World"};
which is equivalent to
Use with AIString[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";
Use with AIString[] args = new String[]{"firstarg", "secondarg", "thirdarg"};