Declare an array in java without size
Couple of things, as people have said regular arrays in Java must be declared with a "new" statement as well as a size. You can have an empty array if you want, but it will be of size 0.
Now, if you want a dynamic array you can use an ArrayList. Its syntax is as follows:
ArrayList<Primitive_Type_Here> = new ArrayList<Primitive_Data_Type_Here>();
Now, once again if you do not tell the array what to have then there is no point in even doing anything with either array type. For ArrayLists you would have to tell it to add any object that fits its type. So for example I can add "Hello World" into an ArrayList we can do the following code.
ArrayList<String> sList = new ArrayList<String>();
sList.add("Hello World");
System.out.println("At index 0 our text is: " + sList.get(0));
//or if you want to use a loop to grab an item
for(int i = 0, size = sList.size(); i < size; i ++)
{
System.out.println("At index " + i + " our text is: " + sList.get(i));
}
**If you are wondering why I am using .size() instead of .length is because arrayLists do not have a .length method, and .size does the same thing for it.
**if you are wondering why I have int = 0, size = sList.size(); it is because this way you enhance your code performance as your loop is of O(n) complexity, however if we were to do i < sList.size() it would be O(n) * sList.size() as you would constantly be calling sList.size() per iteration of your loop.
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.
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");
Why are you using nested loops on 1D-Arrays? This is the problem when you have N-arrays
. It makes your codes unreadable and hard to maintain.
As other posts has mentioned, use arrayList.
Alternatively, if for any reasons, you cannot use ArrayList. At least make your initial array size large enough to handle all scenarios. This way, you don't need to worry about stretching your array's size.
I also noticed your for-loop for printing the array looks bulky.
To print an array, you could:
for (int i=0; i<myarray.length; i++) //throw away the = and the -1
System.out.print(myarray[i] + ",");
OR
for (int i : myarray) //use a for-each loop
System.out.print(i + ",");
OR
System.out.println(Arrays.toString(myarray));