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?
java - How do you declare a string array of unknown size? - Stack Overflow
Declare an array in java without size - Stack Overflow
Is it possible to declare an array without initial size?
How to pass an array of unknown size to a class?
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.
There is a NullPointerException because you declared but never initialized the array.
You can dynamically declare an array as shown below.
int size = 5; // or anyother value you want
int[] array = new int[size];
Or you use a list. Which allows to dynamically change the size. E.g:
List<Integer> list = new ArrayList<>();
list.add(5); //adds number 5 to the list
int number = list.get(0); // Returns Element which is located at position 0 (so in this example in number will be "5");
i do not want the array to have a specific size because each time the size must be different.
Arrays in Java have fixed size. Once declared, you cannot change it. If you try to force you way. You are creating a new array each time. Please do not create array1 to arrayN for any reasons.
If you want dynamic size, which can stretch and shrink. You are looking for ArrayList. ArrayList in Java is easy to use.
ArrayList<Type> arrayList = new ArrayList<>();
or
List<Type> arrayList = new ArrayList<>();
but when am trying the below code there is an error on myarray5
And I guess that would be an ArrayIndexOutOfBoundsException because your loops based on array1's length, but your other arrays like array5 is having different length.