Reputation: 1093
Hello am trying to declare an array in java but i do not want the array to have a specific size because each time the size must be different.
I used this declaration: int[] myarray5;
but when am trying the below code there is an error on myarray5
for(int i=0; i<=myarray1.length - 1; i++){
for (int j=0; j<=myarray2.length - 1; j++){
if (myarray1[i] == myarray2[j]){
myarray5[k] = myarray2[j];
k++;
}
}
}
and also when am printing the array:
for (int i=0; i<=myarray3.length-1; i++){
System.out.print(myarray3[i]+",");
}
Upvotes: 20
Views: 206726
Reputation: 1
The array should be declared outside the method and have the array passed in as a parameter to the method i.e.
methodName(int[] anySizeArray){
//TODO your code here!;
}
Your program should be now able to handle an array of any size.
Upvotes: 0
Reputation: 3164
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");
Upvotes: 35
Reputation: 538
Well what you need is list. Array has to have size because how arrays work.
This is VERY oversimplified explanation:
When you declare an array of size 5
JVM reservs 5 spots for that array and memorize only pointer to first element.
3rd element of that array JVM goes for pointer to 1st element + 3.
List, on the other hand keeps pointer to the first element, and in each element pointer to the next one. So if you want 5th elemnt JVM goes to the 1st element and in it finds memory location of 2nd, in second element find location of 3rd element.
Conclusion: Ordinating trough array is much faster but it has to be fixed size, ordinating trough list is slower but you can add and remove elements as you wish.
And your problem is that probably you don't declare size of your array or you don't initialize your array at all.
Upvotes: -1
Reputation: 3048
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.
Upvotes: 2
Reputation: 17454
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));
Upvotes: 1
Reputation: 17454
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.
Upvotes: 7