You should use a List for something like this, not an array. As a general rule of thumb, when you don't know how many elements you will add to an array before hand, use a List instead. Most would probably tackle this problem by using an ArrayList.
If you really can't use a List, then you'll probably have to use an array of some initial size (maybe 10?) and keep track of your array capacity versus how many elements you're adding, and copy the elements to a new, larger array if you run out of room (this is essentially what ArrayList does internally). Also note that, in the real world, you would never do it this way - you would use one of the standard classes that are made specifically for cases like this, such as ArrayList.
You should use a List for something like this, not an array. As a general rule of thumb, when you don't know how many elements you will add to an array before hand, use a List instead. Most would probably tackle this problem by using an ArrayList.
If you really can't use a List, then you'll probably have to use an array of some initial size (maybe 10?) and keep track of your array capacity versus how many elements you're adding, and copy the elements to a new, larger array if you run out of room (this is essentially what ArrayList does internally). Also note that, in the real world, you would never do it this way - you would use one of the standard classes that are made specifically for cases like this, such as ArrayList.
I think you need use List or classes based on that.
For instance,
ArrayList<Integer> integers = new ArrayList<Integer>();
int j;
do{
integers.add(int.nextInt());
j++;
}while( (integers.get(j-1) >= 1) || (integers.get(j-1) <= 100) );
You could read this article for getting more information about how to use that.
You can't... an array's size is always fixed in Java. Typically instead of using an array, you'd use an implementation of List<T> here - usually ArrayList<T>, but with plenty of other alternatives available.
You can create an array from the list as a final step, of course - or just change the signature of the method to return a List<T> to start with.
Use LinkedList instead. Than, you can create an array if necessary.
Java: init array -- but don't know size - Get Started - SitePoint Forums | Web Development & Design Community
Declare an array in java without size - Stack Overflow
java - How can I initialize an array without knowing it size? - Stack Overflow
java - declare an array with unknown size without using ArrayList - Stack Overflow
Videos
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.
You can't... an array's size is always fixed in Java. Typically instead of using an array, you'd use an implementation of List<T> here - usually ArrayList<T>, but with plenty of other alternatives available.
You can create an array from the list as a final step, of course - or just change the signature of the method to return a List<T> to start with.
Use LinkedList instead. Than, you can create an array if necessary.
Well, in your use case, you know exactly how many numbers you need. Look up how to find the number of odd numbers between two number based on your minimum and maximum. Then just allocate that many:
int closestMin = minimum % 2 == 0 ? minimum + 1 : minimum;
int closestMax = maximum % 2 == 0 ? maximum - 1 : maximum;
int numberOfOdds = ((closestMax - closestMin) / 2) + 1;
int[] arr = new int[numberOfOdds];
....
You can find out how many elements will be stored in the array then construct the array to be that size:
import java.util.Arrays;
public class Practice {
static int[] oddNumbers(int minimum, int maximum) {
int x = 0;
for(int i = minimum; i <= maximum; i++){ //
if(i % 2 != 0){ ////
++x; ////// Find the element count
} ////
} //
int[] arr = new int[x]; // Construct array with the length of the element count
x = 0; // Reset X (Just to avoid creating a new control variable)
for(int i = minimum; i <= maximum; i++){
if(i % 2 != 0){
arr[x] = i;
++x;
}
}
return arr;
}
public static void main(String[] args) {
int min = 3, max = 9;
System.out.println(Arrays.toString(oddNumbers(min, max)));
}
}
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?
I have participated in many coding competitions but I'm unable to pass all the test cases just because of this problem. I don't know how to initialize an array with unknown size. I tried using
arr=malloc(size*sizeof(int));
But at the end it shows segmentation fault. Can someone explain why?