【问题标题】:How to delete last element in array and put new in front?如何删除数组中的最后一个元素并将新元素放在前面?
【发布时间】:2020-05-29 13:29:19
【问题描述】:

我有一个关于 Java 的问题。我刚开始接触 Java,我的谷歌搜索带来了很多结果,但最终的帮助不是。 我创建了一个类来跟踪历史信息。我在不同的日子有不同的价值观,需要定期更新它们。我想跟踪过去 30 天并创建了一个包含 30 个元素的数组。当我调用我的“移位”函数时,我想删除最后的 n 个元素并将零放在前面。这是一个 5 天的小例子:

public class Testclass {

    private int[] histInfo;

    public Element()
    {
        this.histInfo = new int[5];
    }

    public void shift_histInfo(long m)
    {
        //do magic 
    }
}

我想做的事情是

INPUT:
histInfo = [50,21,1,45,901]

OPERATION:
shift_histInfo(2);

RESULT:
histInfo = [0,0,50,21,1]

如果您认为有更优雅或更有效的方法,我很感谢您可以支持的各种帮助以及发人深省的冲动。

最好的:-)

【问题讨论】:

  • 如果你想使用数组(你也可以使用Collection,比如List),你应该使用索引。否则,您将不得不不必要地复制值。
  • 这不是一个困难的问题,我们想看看你到目前为止已经尝试过什么。发布您到目前为止编写的任何代码。
  • @ArvindKumarAvinash:感谢您的回复。我不能给你工作代码,但我试图用内置函数'shift()'来解决这个问题。我想可能有一个构建函数来解决这个问题,否则它可能会变得非常低效。

标签: java arrays shift


【解决方案1】:

除非有非常严格的性能限制,否则使用标准 Collection 类可以完成工作。看看 java.util.LinkedList。

作为编程练习,您可能会考虑创建一个环形缓冲区。这个想法是避免在每次插入时复制数组。

保留一个最旧的索引值。

编写时只需替换 item[oldestIndex] 并增加 oldIndex。

要从最旧的索引开始进行迭代,并使用增量方法处理环绕到数组开头的循环。

int nextIndex(int current) {
   return (current + 1) % arrayLength;
}

编写一个很好的封装类来隐藏所有这些将是一个很好的练习。

【讨论】:

    【解决方案2】:

    你可以试试这个:

    public static void shift_histInfo(long m)
    {
        int[] myIntArray = {50,21,1,45,901};
        int[] myIntArray2 = {50,21,1,45,901};
    
        for (int j=0 ;j< myIntArray.length ; j++){
            int temp = (int) (j+m);
            if (temp >= myIntArray.length){
                temp = temp - myIntArray.length;
                myIntArray2[temp] = 0;
            } else {
                myIntArray2[temp] = myIntArray[j];
            }
    
        }
        for (int j=0 ;j< myIntArray2.length ; j++){
            System.out.println(myIntArray2[j]);
        }
    
    }
    

    输出:

    当shift_histInfo(2),

    [0,0,50,21,1]
    

    【讨论】:

      【解决方案3】:
          int[] array={1,2,3,4,5,6};
          int removelength=2;
          int e=1;
          while(e<=removelength) {
              for(int i=1;i<array.length;i++)
              array[array.length-i]=array[array.length-i-1];
              e++;
          }
      
          for(int i=0;i<removelength;i++) {
              array[i]=0;
          }
          for(int g:array)
          {
            System.out.print(g);
          }
      

      【讨论】:

      • 这样做的一个缺点是问题暗示了一个 int 数组,而不是 List。但它确实突出了为什么集合通常是比数组更好的选择——你可以使用方便的方法,比如旋转 :)
      【解决方案4】:

      对于您想要的约束,尽管我确实使用相同的方法而不是 Element() 初始化了数据。我不知道为什么参数是long 类型所以我留下它并创建了一个int 局部变量。

      它所做的只是将索引值复制到从 m 开始的新数组中,然后递增/迭代直到数组末尾。

      您也可以让方法返回类型int[],然后简单地返回changedInfo 数组。而不是histInfo = changedInfo.clone();

      private int[] histInfo;
      
          public void shift_histInfo(long m) {
              int n = (int) m;
              this.histInfo = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15};
      
              int length = this.histInfo.length;
              int[] changedInfo = new int[length];
      
              if (length - n >= 0) System.arraycopy(histInfo, 0, changedInfo, n + 0, length - n); //Edit: shortened to one line.
      
              histInfo = changedInfo.clone();
              System.out.println("Remove: " + n + " - " + Arrays.toString(changedInfo) + "\n");
      
          }
          public static void main(String[] args) {
              Main main = new Main();
              main.shift_histInfo(0);
              main.shift_histInfo(30);
              main.shift_histInfo(1);
              main.shift_histInfo(15);
              main.shift_histInfo(29);
          }
      

      打印:

      Remove: 0 - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
      
      Remove: 30 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      
      Remove: 1 - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
      
      Remove: 15 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
      
      Remove: 29 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 2020-07-04
        • 2012-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多