Reputation: 37
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
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
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.
Upvotes: 0
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
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
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