-1

I'm new to java and at some points, I had to delete half or some part of an array!

Also, I know we can delete an element by index or looping in an array of integers in java.

However, I would like to know is it possible to delete some part of a sorted array in java in one iteration trying to avoid looping in array? let say we have an array of integers like [ 1, 3, 4, 7, 8, 10, 15]

then I want to delete from 7 to 15

OR delete from 1 to 7

can I delete all the elements 7,8,10,15 in one iteration without looping?

if there is one can you show me an example!

thank you so much.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
advertise bs
  • 3
  • 1
  • 3
  • giving some more context would help you choosing the right way to do it. Are you using integers only? objects? what is the goal of using an array instead of a list? – Pipo Apr 21 '19 at 01:07
  • yes i'm using only integers ! – advertise bs Apr 21 '19 at 19:59
  • i will compare an integer with the middle index in the given sorted array if the number is smaller than the middle index i will need to delete all the elements after the middle index ! – advertise bs Apr 21 '19 at 20:07
  • so definitly use a linkedList which doesn't reallocate and copy again all values. I recommend you to add your previous comment as an edit to your question. See my edit – Pipo Apr 21 '19 at 20:22
  • unfortunately i cant use link-list it has to be an array ! but like you said using CopyOfRange will reallocate and copy again all the values ! – advertise bs Apr 22 '19 at 00:22
  • could you explain why it has to be an array? dont forget to accept my answer if it helped – Pipo Apr 22 '19 at 00:30
  • all the other method that using the result of this getting array as input – advertise bs Apr 23 '19 at 00:36
  • list to array is a simple operation toArray if you own business is doing operations that cost more on array than list its more adecuate to create and manipulate a linkedList and then call its toArray function to give it... depending on the context I cannot see... again see doc for toArray function – Pipo Apr 23 '19 at 01:05

2 Answers2

0

I'd use the copyOfRange funcion the arrays are structures which need memory allocations for modifications so you can either implement a function which moves the desired items over the undesired ones and then remove the unwanted ones. But it seems a heavy operation for an array.

So you should consider:

  • ask yourself if a list is not preferable for what you need as removing various elements is more adecuate from this type of collections

  • implement it yourself if the array structure you are using is really the adecuate one with copyOfRange and fill functions

Edit

As the javadoc for LinkedList points out you could do something like

myList.removeRange(myList.indexOf(yourSearchedInt), myList.size()) //don't remember in Java if you should use size() - 1...
Pipo
  • 4,653
  • 38
  • 47
0

I'm not sure what you mean by "one iteration", since you also talk about looping. But I'm gonna go ahead and assume that you mean that you want to create such a subarray without looping through the array itself. Here is way to achieve that:

private Integer[] subarray(Integer[] array, Integer lowestValue, Integer highestValue) {
    return Arrays.copyOfRange(array,
                              Arrays.binarySearch(array, lowestValue),
                              Arrays.binarySearch(array, highestValue) + 1);
}

Using this method, this code:

Integer[] array = { 1, 3, 4, 7, 8, 10, 15 };

System.out.println(Arrays.toString(subarray(array, 1, 7)));
System.out.println(Arrays.toString(subarray(array, 7, 15)));
System.out.println(Arrays.toString(subarray(array, 4, 10)));

Will produce:

[1, 3, 4, 7]
[7, 8, 10, 15]
[4, 7, 8, 10]
Tobb
  • 11,850
  • 6
  • 52
  • 77