【问题标题】:shifting elements in an array to the right, returning second largest, removing elements (beginning java)将数组中的元素向右移动,返回第二大,删除元素(开始 java)
【发布时间】:2013-12-06 03:23:15
【问题描述】:

通过完成下面的 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 似乎什么都不做,其他两种方法在表示 values[i+1] 的行中给出了超出范围的错误

【问题讨论】:

  • 哪种方法会出现哪种问题
  • 你试过调试吗?
  • removemiddleelement 什么都不做,另外两个产生越界错误,特别是它说 values[i +1]
  • 好消息我得到了第二大的工作! :) 仍然与其他人有点挣扎
  • 得到 shiftRight 工作!

标签: java arrays methods


【解决方案1】:
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 ));
            }
    }

【讨论】:

    【解决方案2】:

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

    右移方法

    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 */
    

    } //结束右移

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      相关资源
      最近更新 更多