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

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

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

问题

以下是翻译好的部分:

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

  1. public class Testclass {
  2. private int[] histInfo;
  3. public Element()
  4. {
  5. this.histInfo = new int[5];
  6. }
  7. public void shift_histInfo(long m)
  8. {
  9. // 进行操作
  10. }
  11. }

我希望 shift_histInfo 函数的操作是:

  1. 输入
  2. histInfo = [50,21,1,45,901]
  3. 操作
  4. shift_histInfo(2);
  5. 结果
  6. 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:

  1. public class Testclass {
  2. private int[] histInfo;
  3. public Element()
  4. {
  5. this.histInfo = new int[5];
  6. }
  7. public void shift_histInfo(long m)
  8. {
  9. //do magic
  10. }
  11. }

What I want shift to do is

  1. INPUT:
  2. histInfo = [50,21,1,45,901]
  3. OPERATION:
  4. shift_histInfo(2);
  5. RESULT:
  6. 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 开始,并使用增量方法来处理回绕到数组开头。

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

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

英文:

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.

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

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

答案2

得分: 0

  1. public static void shift_histInfo(long m)
  2. {
  3. int[] myIntArray = {50, 21, 1, 45, 901};
  4. int[] myIntArray2 = new int[myIntArray.length];
  5. for (int j = 0; j < myIntArray.length; j++) {
  6. int temp = (int) (j + m);
  7. if (temp >= myIntArray.length) {
  8. temp = temp - myIntArray.length;
  9. myIntArray2[temp] = 0;
  10. } else {
  11. myIntArray2[temp] = myIntArray[j];
  12. }
  13. }
  14. for (int j = 0; j < myIntArray2.length; j++) {
  15. System.out.println(myIntArray2[j]);
  16. }
  17. }

输出:

当调用 shift_histInfo(2) 时,

  1. [0, 0, 50, 21, 1]
英文:

You can try this :

  1. public static void shift_histInfo(long m)
  2. {
  3. int[] myIntArray = {50,21,1,45,901};
  4. int[] myIntArray2 = {50,21,1,45,901};
  5. for (int j=0 ;j&lt; myIntArray.length ; j++){
  6. int temp = (int) (j+m);
  7. if (temp &gt;= myIntArray.length){
  8. temp = temp - myIntArray.length;
  9. myIntArray2[temp] = 0;
  10. } else {
  11. myIntArray2[temp] = myIntArray[j];
  12. }
  13. }
  14. for (int j=0 ;j&lt; myIntArray2.length ; j++){
  15. System.out.println(myIntArray2[j]);
  16. }
  17. }

Output :

when shift_histInfo(2) ,

  1. [0,0,50,21,1]

答案3

得分: 0

  1. int[] array = {1, 2, 3, 4, 5, 6};
  2. int removelength = 2;
  3. int e = 1;
  4. while (e <= removelength) {
  5. for (int i = 1; i < array.length; i++)
  6. array[array.length - i] = array[array.length - i - 1];
  7. e++;
  8. }
  9. for (int i = 0; i < removelength; i++) {
  10. array[i] = 0;
  11. }
  12. for (int g : array) {
  13. System.out.print(g);
  14. }
英文:
  1. int[] array={1,2,3,4,5,6};
  2. int removelength=2;
  3. int e=1;
  4. while(e&lt;=removelength) {
  5. for(int i=1;i&lt;array.length;i++)
  6. array[array.length-i]=array[array.length-i-1];
  7. e++;
  8. }
  9. for(int i=0;i&lt;removelength;i++) {
  10. array[i]=0;
  11. }
  12. for(int g:array)
  13. {
  14. System.out.print(g);
  15. }

答案4

得分: 0

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

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

println:

  1. 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]
  2. 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]
  3. 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]
  4. 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]
  5. 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:

确定