Delete Item from Array and Shrink Array

Delete some items from an array and shrink array in Java

You can't shrink an array, you need to create a new one with the new size. You could do something like:

public double[] getShinkedArray(final double[] my_input_array, final int... positions) {
double[] outputArray = new double[my_input_array.length - positions.length];
for (int i = 0, j = 0; i < my_input_array.length; i++) {
if (Arrays.binarySearch(positions, i) < 0) {
outputArray[j++] = my_input_array[i];
}
}
return outputArray;
}

how to delete a value from an array and shrink the array?

You almost have it.

public static int[] shrinkArray(int key)
{
int[] resultArray = new int[myArray.length-1]; //One length less as we removed an item
boolean found = false;
for(int i = 0, j = 0; i < myArray.length; i++, j++)
{
if(!found && myArray[i] == key){ //if we find item first time
i++; //skip it
found = true; //we found first occurrence
}
if(j < resultArray.length)
resultArray[j] = myArray[i]; //copy array
}
if(found)
return resultArray;
return myArray; //not found
}

remove an element of an array and resize it

you should use Array.splice(position_you_want_to_remove, number_of_items_to_remove)

So if you have

   var a  = [1, 2, 3];
a.splice(0, 1); // remove one element, beginning at position 0 of the array
console.log(a); // this will print [2,3]

using reduce to remove item from array

The second argument of reduce is the initial value, i.e. the initial value of acc. Since you're calling a push method on acc, it should probably be an array.

When you omit the second argument, the first element in your array is used as the initial value. I.e.: the first call takes (mapMarkers[0], mapMarkers[1]).

To fix this issue, pass a new array to the reduce method:

mapMarkers = mapMarkers.reduce((acc, curr) => {             
if (curr.sector !== dataSetSector) acc.push(curr);
return acc;
}, []);
// ^--- change

Alternatively, as suggested in the comments, you can use filter.

Why don't arrays in Javascript resize when you remove an item?

Your comparison of delete to RemoveAt and such is incorrect. delete doesn't do what they do; to do that, you'd use splice:

var myArray = ['a', 'b', 'c']myArray.splice(0, 1);console.log(myArray.length) // Prints '2'

Deleting array elements in JavaScript - delete vs splice

delete will delete the object property, but will not reindex the array or update its length. This makes it appears as if it is undefined:

> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined

Note that it is not in fact set to the value undefined, rather the property is removed from the array, making it appear undefined. The Chrome dev tools make this distinction clear by printing empty when logging the array.

> myArray[0]
undefined
> myArray
[empty, "b", "c", "d"]

myArray.splice(start, deleteCount) actually removes the element, reindexes the array, and changes its length.

> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]

How to delete elements in an array an reduce its size?

I'm sorry that this probably isn't going to be what you want to hear, but in java, arrays are technically fixed and size cannot be directly changed.

There are ways around it, and the method (not a java method!) that I suggest would be to use a linked list data structure. It sounds intimidating, but there's tons of help on YouTube when it comes to java.

In a nutshell, it's a way of letting you connect "item types" together in whatever order that you want. One item is either in front of an item or behind another item, so it's similar to an array in this sense.

Using a linked list would also allow you to add zeros to the end of your data as well as giving you the freedom of being able to manipulate the content in your data without the concern of size.

I really hope this helped, I'm sorry if it wasn't much. Teaching data structures in a single comment wouldn't be an easy feat, haha!

Best of luck!

How do you delete an element from an array without creating a new array or using ArrayLists? java

There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add(), remove() or search an element in Array. This is the reason Collection classes like ArrayList and HashSet are very popular.

Though

Thanks to Apache Commons Utils, You can use there ArrayUtils class
to remove an element from array more easily than by doing it yourself.
One thing to remember is that Arrays are fixed size in Java, once you
create an array you can not change their size, which means removing or
deleting an item doesn't reduce the size of the array. This is, in
fact, the main difference between Array and ArrayList in Java.

What you need to do is create a new array and copy the remaining content of this array into a new array using System.arrayCopy() or any other means. In fact, all other API and functions you will use do this but then you don't need to reinvent the wheel.


    import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

....
//let's create an array for demonstration purpose
int[] test = new int[] { 101, 102, 103, 104, 105};

System.out.println("Original Array : size : "
+ test.length );
System.out.println("Contents : " + Arrays.toString(test));

// let's remove or delete an element from an Array
// using Apache Commons ArrayUtils
test = ArrayUtils.remove(test, 2); //removing element at index 2

// Size of an array must be 1 less than the original array
// after deleting an element
System.out.println("Size of the array after
removing an element : " + test.length);
System.out.println("Content of Array after
removing an object : " + Arrays.toString(test));


Related Topics



Leave a reply



Submit