user2958597
user2958597

Reputation: 37

Removing middle elements from an array

For a programming assignment we have to create array methods that carry out different tasks. For this one, I had to remove the middle element if the length of the array is odd, or the two middle elements if the length of the array is even.

Below is my method body. (values is the name for the pre-established array). On both lines where it says " int[] copy = new int[copy.length-1];" I'm getting an error "local variable copy may not have been initialized" If you have any ideas on how to fix that or see any other glaring errors I would really appreciate your input :) Thanks

public void removeMiddleElement(){
     int count = 0;
     for(int i=0; i<values.length; i++){
         count++;
     }
     if(count%2==0){
         int middle1=count/2;
         int middle2=(count/2)+1;
         int[] copy = new int[copy.length-1];
         System.arraycopy(copy, 0, copy, 0, middle1);
         System.arraycopy(copy, middle1+1, copy, middle1, copy.length-middle1-1);
         System.arraycopy(copy, 0, copy, 0, middle2);
         System.arraycopy(copy, middle2+1, copy, middle2, copy.length-middle2-1);
         copy = values; 
        }
     else if(count%2!=0){
         int middle3=(int) ((count/2)+.5);
         int[] copy = new int[copy.length-1];
         System.arraycopy(copy, 0, copy, 0, middle3);
         System.arraycopy(copy, middle3+1, copy, middle3, copy.length-middle3-1);
         copy = values;
         }

Upvotes: 0

Views: 2836

Answers (5)

MaSa
MaSa

Reputation: 13

My answer:

public void removeMiddleElement(){
     int count = 0;
     for(int i=0; i<values.length; i++){
         count++;
     }
     if(count%2==0){
         int middle1=count/2;
         int middle2=(count/2)+1;
         int[] copy = new int[values.length-1];
         System.arraycopy(copy, 0, copy, 0, middle1);
         System.arraycopy(copy, middle1+1, copy, middle1, copy.length-middle1-1);
         System.arraycopy(copy, 0, copy, 0, middle2);
         System.arraycopy(copy, middle2+1, copy, middle2, copy.length-middle2-1);
         copy = values; 
        }
     else if(count%2!=0){
         int middle3=(int) ((count/2)+.5);
         int[] copy = new int[values.length-1];
         System.arraycopy(copy, 0, copy, 0, middle3);
         System.arraycopy(copy, middle3+1, copy, middle3, copy.length-middle3-1);
         copy = values;
         }
}

actually I used array list :

public int[] removeMiddle(){

ArrayList<Integer> arrayList = new ArrayList<Integer>();
if (values.length%2 == 0) {
for (int i=0;i<values.length/2-1;i++)arrayList.add(values[i]);
for (int i=values.length/2+1;i<=values.length-1;i++)arrayList.add(values[i]);
} else
{
for (int i=0;i<values.length/2;i++)arrayList.add(values[i]);
for (int i= (values.length/2+1);i<=values.length-1;i++)arrayList.add(values[i]);
}
int[] array = new int[arrayList.size()];
for (int i=0; i <= array.length-1; i++)
    {
        array[i] = arrayList.get(i);
    }
    return array; 
}

Upvotes: 0

HLP
HLP

Reputation: 2212

Might want to try something like

    int[] values = { 1,2,3,4,5,6 };

    int halfSize = (int) (values.length/2f-0.5);

    int[] newValues = new int[halfSize*2];
    System.arraycopy(values, 0, newValues, 0, halfSize);
    System.arraycopy(values, values.length-halfSize, newValues, halfSize, halfSize);

    System.out.println(halfSize);
    for (int i = 0; i < newValues.length; i++) {
        System.out.println(newValues[i]);
    }

This will print 1, 2, 5, 6. If you had only 5 items, it would have print 1, 2, 4, 5

The trick here is in halfSize. If you have 3 element: 3/2-0.5 = 1. If you have 4 element: 4/2-0.5 = 1.5 = 1 once converted to int.

  • So in both case (3 elements and 4 elements) we only take the first and the last element.
  • If you had 5 elements or 6 elements, halfSize would be 2, and in both case we take the first 2 elements and the last 2 elements.

Upvotes: 0

john
john

Reputation: 1

you havn't initialize copy when you use copy.length, i.e when you use copy.length, copy has not been initialized. you use copy.length in initilize sentence. by the way,copy.length sames variable.length.

Upvotes: 0

Thayne
Thayne

Reputation: 6992

you are trying to use the length of copy in the initial definition of copy, which you can't do. You probably meant:

int[] copy = new int[values.length -1];

Upvotes: 0

Sinkingpoint
Sinkingpoint

Reputation: 7624

You are trying to initialize copy with the length field of copy. This doesn't make sense. The error is because you access copy.length before copy has been initialized. I assume you actually wanted values.length.

Upvotes: 2

Related Questions