Question

I am trying to deal with this problem: "Remove the middle element if the array length is odd, or the middle two elements if the length is even"

I have found the "round" way of solving this problem using ArrayList, but I want to do the same using only Arrays. This is my code, using ArrayLists.

import java.util.*;
public class test{
public static void main (String[]args){
    ArrayList<Integer> outputArray = new ArrayList<Integer>();

    int [] myArr = {5,8,9,10,11,5};
    int midEl = myArr.length/2;
    int midElMinusOne = myArr.length/2 -1;
    for(int i=0;i<myArr.length;i++){
        outputArray.add(myArr[i]);
    }
    outputArray.remove(midEl);
    outputArray.remove(midElMinusOne);


    System.out.print(outputArray);

}
}
Was it helpful?

Solution

As others have already mentioned, you cannot just remove the middle elements from an existing array. But you can however simply calculate the "beginning" and the "end" of the middle (if you want to call it like that) and then copy the left and the right part of the middle into a result array:

int middleStart = (myArr.length - 1) / 2;
int middleEnd = (myArr.length - 1) - middleStart;
int elementsToRemove = middleEnd - middleStart + 1;

int[] result = new int[myArr.length - elementsToRemove];
System.arraycopy(myArr, 0, result, 0, middleStart);
System.arraycopy(myArr, middleEnd + 1, result, middleStart, middleStart);

It might need some additional bounds checking but i don't think that there is any more efficient way to remove the middle element(s) of an array.

OTHER TIPS

  • You cannot delete elements from an array, you need to create new copy from the original one, and then return the result, or change the reference of the original array to the new copy.

    public class Test
    {
        public static void main(String[] args)
        {
        int[] myArray = new int[]{1,2,3,4,5,6,7};
        boolean even = (myArray.length%2==0);
    
        int[] resultArray;
        if(even)
            resultArray = new int [myArray.length-2];
        else 
            resultArray = new int [myArray.length-1];
    
        int mid1 =  myArray.length/2;
        int mid2 = myArray.length/2 - 1;
    
        int index = 0;
        for(int i=0;i<myArray.length;i++)
        {
            if(even && (i==mid1 || i==mid2))
            {
                continue;
            }
            else if(!even && i==mid1)
            {
                continue;
            }
    
            resultArray[index] = myArray[i];
            index++;
        }
    
        printArray(resultArray);
    }
    
    private static void printArray(int[] array)
    {
        for(int i=0;i<array.length;i++)
        {
            System.out.println("array[" + i + "] = " + array[i]);
        }
    }
    

    }

If you just have to remove one element from Array(not arraylist), and you know its index you can use ArrayUtils library

ArrayUtils.remove

and just do

if(myArr.length%2==1)
ArrayUtils.removeElement(myArr, myArr.length/2);
else
{ArrayUtils.remove(myArr,myArr.length/2);
ArrayUtils.remove(myArr,myArr.length/2);
import java.util.*;
public class test{
public static void main (String[]args){


    int [] myArr = {5,8,9,10,11,5};
    int midEl = (myArr.length-1)/2;// array index start with 0   
     int j = midE1;
    if(myArr.length %2 ==0){
      myArr[j] = myArr[midE1+2];
      j++;
      midE1 = midE1+2;
    }   

 for(int i=midEl;i<myArr.length;i++){
        myArr[j] = myArr[i+1];
        j++;
    }
System.out.print(myArr);

}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top