我试图找出如果数组具有偶数长度的数组中间元素,如果数组的长度为奇数则如何去除中间元素。程序在编译时运行,但结果没有任何变化。我也不确定我是否正确使用System.arraycopy方法(第一个位置是原始数组,第二个位置是你要开始复制的位置,第三个是目标数组,第四个是目标数组的起始位置,最后一个位置是要复制的数组元素的数量。这是我到目前为止的代码:
public void removeMiddle(int[] values)
{
//lets say the array size is 10
boolean even = (values.length % 2 == 0);
int middle1 = values.length/2;
int middle2 = values.length/2 - 1;
if(even)
{
int[] copy = new int[values.length - 2];
//copying the elements 0-3 to the new array
System.arraycopy(values, 0, copy, 0, copy.length - middle1 -1);
//copying the last 4 elements to the new array
System.arraycopy(values, middle1 + 1,copy, middle1, copy.length-middle2 - 1);
}
else if(!even)
{
int[] copy = new int[values.length - 1];
//copying elements 0-3
System.arraycopy(copy,0,copy, 0, copy.length - middle1 -1);
System.arraycopy(copy,middle1 +1 ,copy, middle1 + 1, copy.length - middle1 -1 );
}
}
答案 0 :(得分:1)
复制后半部分时索引错误,数组索引从0开始。
public void removeMiddle(int[] values)
{
//lets say the array size is 10
boolean even = (values.length % 2 == 0);
int middle1 = values.length/2;
int middle2 = values.length/2 - 1;
if(even)
{
int[] copy = new int[values.length - 2];
//copying the elements 0-3 to the new array
System.arraycopy(values, 0, copy, 0, copy.length - middle1 -1);
//copying the last 4 elements to the new array
System.arraycopy(values, middle1, copy, middle2, copy.length-middle1 - 1);
}
else
{
int[] copy = new int[values.length - 1];
//copying elements 0-3
System.arraycopy(copy, 0, copy, 0, copy.length - middle1 -1);
System.arraycopy(copy, middle1 ,copy, middle1 , copy.length - middle1 -1 );
}
}
答案 1 :(得分:0)
问题在于,虽然您创建并正确填充了copy
数组,但您永远不会使用或返回它们:
public int[] removeMiddle(int[] values)
{
//lets say the array size is 10
boolean even = (values.length % 2 == 0);
int middle1 = values.length/2;
int middle2 = values.length/2 - 1;
if (even)
{
int[] copy = new int[values.length - 2];
//copying the elements 0-3 to the new array
System.arraycopy(values, 0, copy, 0, copy.length - middle1 -1);
//copying the last 4 elements to the new array
System.arraycopy(values, middle1 + 1,copy, middle1, copy.length-middle2 - 1);
return copy; // <--
}
else
{
int[] copy = new int[values.length - 1];
//copying elements 0-3
System.arraycopy(values, 0, copy, 0, copy.length - middle1 -1);
// ^
System.arraycopy(values, middle1 + 1, copy, middle1 + 1, copy.length - middle1 -1 );
// ^
return copy; // <--
}
}
对于奇数长度,您也不使用正确的数组作为arraycopy
来源。除此之外,您的arraycopy
用法完全正常。
答案 2 :(得分:0)
在算法上,您可以根据需要重新设置前X值和最后X值,其中X是小于一半大小的最大整数。
在计算上,这意味着(size - 1) / 2
的舍入结果。
例如:10 =&gt; 4,9 =&gt; 4,8 =&gt; 3.
这意味着对于10个元素的数组,您需要前4个元素和后4个元素。
public int[] removeMiddle(int[] values) {
int x = (values.length - 1) / 2;
int[] copy = new int[x * 2];
System.arraycopy(values, 0, copy, 0, x);
System.arraycopy(values, values.length - x, copy, x, x);
return copy;
}
答案 3 :(得分:0)
所以我弄清楚问题是什么。我需要将我的testValue分配给main方法中的新变量。这是最终的答案。再次感谢大家的帮助:
主要方法:
//测试removeMiddle()
System.out.println("Before call to removeMiddle() ");
printArray(testValues1);
System.out.println("After call to removeMiddle() ");
testValues = myMethods.removeMiddle(testValues1);
printArray(testValues);
System.out.println();
//方法类
public int [] removeMiddle(int [] values) {
boolean even = (values.length % 2 == 0);
int middle1 = values.length/2;
int middle2 = values.length/2 - 1;
if (even)
{
int[] copy = new int[values.length-2];
//copying the elements 0-3 to the new array
System.arraycopy(values, 0, copy, 0, copy.length/2);
//copying the last 4 elements to the new array
System.arraycopy(values, middle1+1,copy, middle2, copy.length/2);
return copy;
}
else
{
int[] copy = new int[values.length-1];
//copying elements 0-3
System.arraycopy(values, 0, copy, 0, copy.length/2-1);
System.arraycopy(values, middle1 + 1, copy, middle1 + 1, copy.length/2-1);
return copy;
} }