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

huangapple go评论140阅读模式
英文:

How to delete last element in array and put new in front?

问题

以下是翻译好的部分:

我对Java有一个问题。我刚开始学习Java,我的谷歌搜索得到了许多结果,但没有一个是最终的帮助。
我创建了一个类来跟踪历史信息。我有不同的值对应不同的天数,需要定期更新它们。我想要跟踪最近的30天,并创建了一个有30个元素的数组。当我调用我的 'shift' 函数时,我想丢弃最后n个元素,并在前面插入零。以下是一个包含5天的简单示例:

public class Testclass {

    private int[] histInfo;

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

    public void shift_histInfo(long m)
    {
        // 进行操作
    }
}

我希望 shift_histInfo 函数的操作是:

输入
histInfo = [50,21,1,45,901]

操作
shift_histInfo(2);

结果
histInfo = [0,0,50,21,1]

如果您认为有更加优雅或高效的方法,我将非常感激您提供的任何帮助和富有启发性的思路。

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

英文:

I have a question concerning Java. I started up new to Java and my google search brought many results but non was the final help.
I created a class to track historical information. I have different values for different days and need to update them un a regular basis. I want to keep track of the last 30 days and created an array with 30 elements. When I call my 'shift' function I want to drop the last n elements and put zeros in front. Here is a minial example for 5 days:

public class Testclass {

    private int[] histInfo;

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

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

What I want shift to do is

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

OPERATION:
shift_histInfo(2);

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

I am thankfull for every kind of help you can support as well for thought-provoking impulses if you think that there is a way more elegant or efficient way.

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

答案1

得分: 1

除非有非常严格的性能限制,使用标准的集合类就能完成任务。看一下 java.util.LinkedList。

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

保持一个 oldestIndex 值。

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

要进行迭代,从 oldestIndex 开始,并使用增量方法来处理回绕到数组开头。

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

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

英文:

Unless there are very tight performance constraints using the standard Collection classes will get the job done. Have a look at java.util.LinkedList.

As a programming exercise you might consider creating a ring buffer. The idea being to avoid copying the array on every insertion.

Keep a oldestIndex value.

When writing simply replace item[oldestIndex] and increment oldestIndex.

To iterate you start at oldestIndex and use an increment method to deal with wrapping round to the start of the array.

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

Writing a nice encapsulating class to hide all this would be a good exercise.

答案2

得分: 0

public static void shift_histInfo(long m)
{
    int[] myIntArray = {50, 21, 1, 45, 901};
    int[] myIntArray2 = new int[myIntArray.length];

    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]
英文:

You can try this :

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&lt; myIntArray.length ; j++){
        int temp = (int) (j+m);
        if (temp &gt;= myIntArray.length){
            temp = temp - myIntArray.length;
            myIntArray2[temp] = 0;
        } else {
            myIntArray2[temp] = myIntArray[j];
        }

    }
    for (int j=0 ;j&lt; myIntArray2.length ; j++){
        System.out.println(myIntArray2[j]);
    }

}

Output :

when shift_histInfo(2) ,

[0,0,50,21,1]

答案3

得分: 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);
}
英文:
    int[] array={1,2,3,4,5,6};
    int removelength=2;
	int e=1;
	while(e&lt;=removelength) {
		for(int i=1;i&lt;array.length;i++)
		array[array.length-i]=array[array.length-i-1];
		e++;
	}
	
	for(int i=0;i&lt;removelength;i++) {
		array[i]=0;
	}
    for(int g:array)
    {
      System.out.print(g);
    }

答案4

得分: 0

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

    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]
英文:

For constraints that you wanted, although I did initialise the data in the same method instead of Element(). I don't know why the parameter is of type long so I left it and made an int local variable.

All it does is copy the index value over to the new array starting at m then increments/iterates until the end of the array.

You can also make the method return type int[] and then simply return changedInfo array. Instead of 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 &gt;= 0) System.arraycopy(histInfo, 0, changedInfo, n + 0, length - n); //Edit: shortened to one line.

        histInfo = changedInfo.clone();
        System.out.println(&quot;Remove: &quot; + n + &quot; - &quot; + Arrays.toString(changedInfo) + &quot;\n&quot;);

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

huangapple
  • 本文由 发表于 2020年5月29日 21:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/62087127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定