If the array is an int array you can do
for(int i=0; i < array.length; i++)
if(array[i] == 0) {
array[i] = newValue;
break;
}
if it is an Object array you can do
for(int i = 0; i < array.length; i++)
if(array[i] == null) {
array[i] = newObject;
break;
}
Answer from gkrls on Stack OverflowIf the array is an int array you can do
for(int i=0; i < array.length; i++)
if(array[i] == 0) {
array[i] = newValue;
break;
}
if it is an Object array you can do
for(int i = 0; i < array.length; i++)
if(array[i] == null) {
array[i] = newObject;
break;
}
Create the array of size x.
Create a stack of size x which indicates all free indexes. Push all indexes (in reverse order) to the stack.
When you try to add an element to the array pop from the stack the next free index. Use the index to insert to the array.
If an element is removed, push the index back to the stack to indicate that it is free and nullify the element in the array.
If you want to add an element and the stack is empty i.e. the array is full, well you decide what to do.
Your other option would be to loop over the array to find the next "free" spot which would be indicated by a null.
How to add something to primitive array in Java?
java - add elements from one array to "empty array" - Stack Overflow
Java array, add item into next empty index - Stack Overflow
java - How to create an empty array? - Stack Overflow
Videos
Hi all, so I have an empty array I just created like this:
int[] array = new int[5];
How do I add numbers to it? I tried array.append() but it's not working. I don't want to do it manually like array[0] etc I want to just keep adding to the tail.
EDIT: I'm sure I won't go over the limit of what the array can contain. I just want to know how to add to the tail of the array without having to specify what position is being added.
To set the ith element in result to the ith element in newarray, do the following:
result[i] = newarray[i];
In addition, you can copy over the entirety of the array using arraycopy:
System.arraycopy(newarray, 0, result, 0, newarray.length);
See also:
- Arrays - The Javaโข Tutorials
- arraycopy() in Java
Use System.arrayCopy to copy arrays. Example:
System.arraycopy(newarray, 0, result, 0, newarray.length)
The first argument is the source array, then the source position, then the destination array and destination position, and the length.
If you are actually using the array as a stack (and thus, will only add or remove items at the top of the stack) then you could keep in another variable the next free index in the array.
int[] array = new int[21];
int nextIndex = 0;
public void push(int e) {
array[nextIndex] = e;
++nextIndex;
}
public int pop() {
--nextIndex;
return array[nextIndex];
}
If removals can occur anywhere, then I don't see a better solution than iterating over the array to find a free spot.
That is why Listhave been made. Simply use something like this:
List<Integer> negativeIntegers = new ArrayList<Integer>(21);
...
negativeIntegers.add(-127);
You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this:
public void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
BufferedReader console =
new BufferedReader(new InputStreamReader(System.in));
while(true) {
String word = console.readLine();
if (word.equalsIgnoreCase("end") {
break;
} else {
numbers.add(Integer.parseInt(word);
}
}
Ofcourse you won't use while(true)and you won't put this in main, but it's just for the sake of the example
How about:
Scanner scan = new Scanner(System.in);
System.out.print("Enter the array size: ");
int size = scan.nextInt();
int[] yourArray = new int[size];
//can even initialize it
Arrays.fill(yourArray, -1);