如何删除数组中的最后一个元素,并在前面放置新元素?

问题描述 投票:0回答:4

我有一个关于Java的问题。我刚开始接触Java,但Google搜索带来了很多结果,但最终结果不是。我创建了一个班来跟踪历史信息。我在不同的日子使用不同的值,因此需要定期进行更新。我想跟踪过去的30天,并创建了一个包含30个元素的数组。当我调用'shift'函数时,我想删除最后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]

如果您认为有一种更优雅或更有效的方法,您还可以为发人深省的冲动提供各种帮助,我深感感谢。

最佳:-)

java arrays shift
4个回答
1
投票

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

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

保留最旧的索引值。

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

要迭代,请从oldestIndex开始,并使用一个增量方法来处理回合到数组的开头。

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

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


0
投票

您可以尝试这个:

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]

0
投票
    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);
    }

0
投票

对于您想要的约束,尽管我确实使用相同的方法而非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(changedInfo, 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);
    }

println:

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]
© www.soinside.com 2019 - 2024. All rights reserved.