Reputation: 4271
I have a java array. I want to remove first d
items from array and store it to some other array. I am able to store but not able to remove. My Code
private static void itemRemove(int[] inputArr, int d) {
int newArr [] = new int[d];
for(int i=0;i<d;i++){
newArr[i] = inputArr[i];
}
itemPrint(inputArr);
itemPrint(newArr);
}
So in example suppose I have array
inputArr
is [1,2,3,4,5]
and my d
is 2
I am able to add in to newArray
[1,2]
but not able to remove from inputArr
.
Also once I remove two element from inputArr
which is having size 5 so I can add two more element. Can anybody give some idea how to add element.
Upvotes: 2
Views: 10504
Reputation: 452
You can do it using System.arrayCopy and Arrays.fill functions
int[] inputArr = {1,2,3,4,5};
int n = 3; //number of positions to move
int[] newArray = new int[n];//creating new array of size n
System.arraycopy(inputArr, 0, newArray, 0, n);//copying elements up to nth position to new array
System.arraycopy(inputArr, n, inputArr, 0, inputArr.length-n);//copying remaining elements to start position
Arrays.fill(inputArr, inputArr.length-n, inputArr.length, 0);//filling cells after the last element with default 0 value
itemPrint(newArray);
itemPrint(inputArr);
Upvotes: 1
Reputation: 14062
You can do something like this:
public class Test {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6};
System.out.println("Original Array");
itemPrint(array);
itemRemove(array, 2);
array = itemsLeft(array,2);
System.out.println("Original Array After Removing Items");
itemPrint(array);
}
private static void itemRemove(int[] inputArr, int d) {
int newArr [] = new int[d];
for(int i=0;i<d;i++) newArr[i] = inputArr[i];
System.out.println("New Array");
itemPrint(newArr);
}
private static void itemPrint(int[] array) {
for(Integer i : array) System.out.println(i);
}
private static int[] itemsLeft(int[] inputArr, int d) {
int [] itemsLeftArr = new int[inputArr.length-d];
for(int i=d, j=0; i<inputArr.length; i++, j++) {
itemsLeftArr[j] = inputArr[i];
}
return itemsLeftArr;
}
}
Output:
Original Array
1
2
3
4
5
6
New Array
1
2
Original Array After Removing Items
3
4
5
6
Upvotes: 0
Reputation: 44110
You're not doing any removal here:
for(int i=0;i<d;i++){
newArr[i] = inputArr[i];
}
You just copy the the value from the input array to the new array. The input array is unchanged.
Arrays in Java are of a fixed size so there is no way to remove elements. The typical way you can approximate this is to replace the element with some marker (or 'sentinel') value. For objects, this could be null
. For primitive values, such as integers, you might just need to choose a value arbitrarily:
for(int i = 0; i < d; i++) {
newArr[i] = inputArr[i];
inputArr[i] = -999; //marked for removal
}
This is obviously not a very good solution. What if -999 is already in the array?
The correct way to deal with this is to use a List
. List
s are not fixed-size, so it is possible to remove elements. There are multiple implementations you can use. ArrayList
is the closest to an array.
Your function, changed to use an ArrayList
, might look like this:
private static void itemRemove(List<Integer> inputArr, int d) {
List<Integer> newArr = new ArrayList<>();
int removed = 0;
while (removed < d)
{
newArr.add(inputArr.remove(0));
removed++;
}
itemPrint(inputArr);
itemPrint(newArr);
}
There's a few different things going on here. Most notably, we had an int[]
and now we have a List<Integer>
. We're using what's called "generics" and generics don't work with primitive types, so we need to use what's called the "boxed type".
The other noticeable change is that the loop is different. There are difficulties removing from collections while you are iterating over them and I structured the loop differently to avoid this problem.
Upvotes: 0
Reputation: 568
To remove element from Array you need to remove the existing index of that element as well.
As array in java is immutable so you need to re-assign the array values to new array as you are doing already with newArr[index]
Just to remove array elements without altering index you may use the predefined library of commons.apache.org like this:
array = ArrayUtils.removeElement(array, element)
Or, you may use the arraylist instead of this for dynamic sizing of the array.You can initialize your input array like this:
ArrayList<> inputArr= new ArrayList<>();
To remove element from inputArr with index position just use the remove(int index) method of list.
inputArr.remove(your index here);
Upvotes: 0
Reputation: 530
You need to create a new int[]
array of size inputArr.length - d
and copy the values:
private static int[] itemRemove(int[] inputArr, int d) {
int[] newArrd = new int[d];
int[] newArr = new int[inputArr.length-d];
int newArrIdx = 0;
for(int i=0;i<inputArr.length;i++){
if(i<d){
newArrd[i] = inputArr[i];
}else{
newArr[newArrIdx++] = inputArr[i];
}
}
return newArr;
}
Upvotes: 2
Reputation: 61969
Elements cannot be removed from arrays. The only thing you can do is copy other elements to overwrite them. So, if your array looks like this:
1 2 3 4 5
You can copy 3, 4 and 5 down to overwrite 1 and 2, so you will be left with this:
3 4 5 4 5
(note how 4 and 5 are still there)
And then you can overwrite the last two elements with the new elements that you want to store in the array, which will leave you with this:
3 4 5 6 7
To copy elements, you can do it manually with a for
loop, or you can use System.arrayCopy()
.
Upvotes: -1