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

Java 如何删除数组中的最后一个元素并将新元素放在前面?,java,arrays,shift,Java,Arrays,Shift,我有一个关于Java的问题。我刚开始接触Java,我的google搜索带来了很多结果,但最终的帮助是非。 我创建了一个类来跟踪历史信息。我在不同的日子有不同的值,需要定期更新。我想跟踪过去30天,并创建了一个包含30个元素的数组。当我调用“shift”函数时,我想去掉最后的n个元素,并在前面加上零。以下是一个5天的小示例: public class Testclass { private int[] histInfo; public Element() {

我有一个关于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]
如果你认为有一种更优雅或有效的方法,我感谢你所能支持的每一种帮助,以及发人深省的冲动

最佳:-)

您可以尝试以下方法:

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

}
int[]数组={1,2,3,4,5,6};
int-removelength=2;
int e=1;

而(e除非有非常严格的性能限制,否则使用标准集合类将完成任务

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

保留旧的索引值

写入时,只需替换项[OldEstinex]并增加OldEstinex

要进行迭代,请从oldestIndex开始,并使用增量方法处理换行到数组开头的操作

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

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

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

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

int nextIndex(int current) {
   return (current + 1) % arrayLength;
}
您还可以使方法返回类型为
int[]
,然后只返回
changedInfo
array。而不是
histInfo=changedInfo.clone();

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]

如果要使用数组(也可以使用
集合
,如
列表
)你更应该在索引上工作。否则你将不得不不必要地复制值。这不是一个困难的问题,我们想看看你到目前为止都做了些什么。发布你到目前为止写的所有代码。@ArvindKumarAvinash:谢谢你的回复。我不能给你工作代码,但我已经尝试用内置函数解决了这个问题“shift()”。我认为可能有一个构建函数来解决这个问题,否则它可能会变得非常低效。这个问题的一个缺点是,这个问题意味着一个int数组,而不是一个列表。但它确实强调了为什么集合通常比数组好得多-您可以使用方便的方法,如rotate:)
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]