Java 如果数组长度为奇数,请删除中间的元素;如果长度为偶数,请删除中间的两个元素。
我试图弄清楚,如果数组长度为偶数,如何从数组中删除中间元素;如果数组长度为奇数,如何从数组中删除中间元素。程序在编译时运行,但是结果没有任何变化。我也不确定是否正确使用System.arraycopy方法(第一个位置是原始数组,第二个位置是要开始复制的位置,第三个位置是目标数组,第四个位置是目标数组的开始位置,最后一个位置是要复制的数组元素数)以下是我到目前为止的代码:Java 如果数组长度为奇数,请删除中间的元素;如果长度为偶数,请删除中间的两个元素。,java,arrays,Java,Arrays,我试图弄清楚,如果数组长度为偶数,如何从数组中删除中间元素;如果数组长度为奇数,如何从数组中删除中间元素。程序在编译时运行,但是结果没有任何变化。我也不确定是否正确使用System.arraycopy方法(第一个位置是原始数组,第二个位置是要开始复制的位置,第三个位置是目标数组,第四个位置是目标数组的开始位置,最后一个位置是要复制的数组元素数)以下是我到目前为止的代码: public void removeMiddle(int[] values) { //lets say the array
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 );
}
}
问题在于,尽管您创建并正确填充了
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; // <--
}
}
public int[]removemidle(int[]值)
{
//假设数组大小为10
布尔偶数=(values.length%2==0);
int middle1=value.length/2;
int middle2=values.length/2-1;
如果(偶数)
{
int[]copy=newint[values.length-2];
//将元素0-3复制到新阵列
System.arraycopy(值0,copy,0,copy.length-1-1);
//将最后4个元素复制到新数组
System.arraycopy(值,middle1+1,copy,middle1,copy.length-middle2-1);
return copy;//复制后半部分时索引错误,数组索引从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 );
}
}
从算法上讲,您可以根据需要重述前X值和最后X值,其中X是小于一半大小的最大整数。
计算上,这意味着(size-1)/2
的四舍五入结果
例如:10=>4,9=>4,8=>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;
}
所以我找到了问题所在。我需要在main方法中将我的testValue分配给一个新变量。下面是最终的答案。再次感谢大家的帮助:
主要方法:
//测试移除中间()
//方法类
公共int[]removeMidel(int[]值)
{
}
}谢谢大家的帮助,我真的很感激。不幸的是,当我按照这些建议更新代码时,我仍然面临同样的问题。当我编译并运行程序时,结果与初始数组相同(不删除中间元素)。您是否将结果分配给了新变量或替换了调用站点上的旧数组?当在main方法中调用时,它应该替换旧数组。您可以在问题中发布main方法的代码吗?抱歉,else语句中的部分不正确。它应该是:else{int[]copy=new int[values.length-1];//复制元素0-3 System.arraycopy(值,0,copy,0,copy.length/2);System.arraycopy(值,middle1+1,copy,middle1,copy.length/2);return copy;}当,我为这个非常简单的解决方案感到骄傲,没有人喜欢它。:-(我对这个问题最好的答案是,投赞成票:)
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();
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;