将数组中的元素向右移,返回第二大元素,删除元素(从java开始)

时间:2013-12-06 03:23:15

标签: java arrays methods

通过完成下面的ArrayMethods类,编写为整数数组执行以下任务的数组方法:

public class ArrayMethods {
private int[] values;
public ArrayMethods(int[] initialValues) { values = initialValues; }

public void shiftRight() {.......}
public int returnSecondLargest {......}
  • 将所有元素向右移动一个,并将最后一个元素移动到第一个位置。例如,1 4 9 16 25将转换为25 1 4 9 16
  • 返回数组中第二大的元素
  • 如果数组长度为奇数,则删除中间元素;如果长度为偶数,则删除中间两个元素

对于换档方法,我有这个:

public void shiftRight(){
    for(int i=0; i<=values.length-1; i++){
    if(i<values.length){
        values[i]=values[i+1];
    }
    else if(i==values.length+1){
        values[i]=values[0];
    } 
}

对于返回的第二大方法,我有这个:

public int returnSecondLargest(){
    int temp;
    for (int i = 0; i < values.length-1; i++) {
        if(values[i] > values[i+1]){
        temp = values[i];
        values[i] =values[i + 1];
        values[i+1] = temp;
    }
    }
    return values[values.length - 2];
}

对于删除中间元素方法,我有这个:

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

在我的每个方法中,我都遇到了“越界”错误,或者他们似乎什么也没做,并且返回原始数组而没有进行任何更改。如果你看到任何明显的错误或有任何建议,将非常感谢:)谢谢!

编辑:removeMiddleElement似乎什么都不做,另外两个方法在行[i + 1]

的行中给出了超出界限的错误

2 个答案:

答案 0 :(得分:0)

for the array must be sorted
public void shiftRight(){
       int temp  = values[values.length -1];
       if(values.length >= 2)
       {
           System.arraycopy(values,0,values,1, values.length -1);
       }
       values[0] = temp;
    }

第二个函数(还必须对数组进行排序)

public int returnSecondLargest()
    {
        return values[values.length - 2];
    }


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

答案 1 :(得分:0)

对于您的右移方法,请考虑您将从数组的最大索引值开始。因此,不是递增索引(或i),而是从数组的末尾开始递减!由于这样做,你必须在移动东西之前将值存储在max索引处,所以在for循环退出后专用一个整数值,该值将分配给array [0]!

ShiftRight方法

public static void shiftRight(int [] array){

  int max = array[array.length - 1]; /*this value stores the max INDEX (not the value of the MAX index)*/

  /*- i starts at the index before the last index of the array
    - the for loop will exit once i is less than zero
    - keep in mind, we are working with the indexes and not the actual values */

  for (int i = array.length - 2; i >= 0; i--) {
     array[i+1] = array[i]; /* takes the array value at the max index and replaces it with the array index before it */
  }   

  array[0] = max; /*setting the highest index to the first index of the array */

} // end shiftRight