简体   繁体   English

从数组中删除中间元素

[英]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). (values是预先建立的数组的名称)。 On both lines where it says " int[] copy = new int[copy.length-1];" 在两行中,它表示“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;
         }

You are trying to initialize copy with the length field of copy . 您正尝试使用copy的长度字段初始化copy This doesn't make sense. 这没有意义。 The error is because you access copy.length before copy has been initialized. 该错误是因为您在初始化复制之前访问了copy.length。 I assume you actually wanted values.length. 我假设你真的想要values.length。

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];

you havn't initialize copy when you use copy.length, ie when you use copy.length, copy has not been initialized. 当你使用copy.length时,你没有初始化副本,即当你使用copy.length时,copy还没有被初始化。 you use copy.length in initilize sentence. 你在initilize句子中使用copy.length。 by the way,copy.length sames variable.length. 顺便说一句,copy.length与variable.length相同。

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 这将打印1,2,5,6。如果您只有5个项目,则会打印1,2,4,5

The trick here is in halfSize. 这里的诀窍是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. 如果你有3个元素:3 / 2-0.5 = 1.如果你有4个元素:4 / 2-0.5 = 1.5 = 1一旦转换为int。

  • So in both case (3 elements and 4 elements) we only take the first and the last element. 所以在这两种情况下(3个元素和4个元素)我们只采用第一个和最后一个元素。
  • 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. 如果您有5个元素或6个元素,则halfSize将为2,在这两种情况下,我们将采用前2个元素和最后2个元素。

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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM