java ArrayList 删除第一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30205529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:39:31  来源:igfitidea点击:

ArrayList Removing first element

java

提问by Bill Cheng

This is the given question: Given a non-negative number represented as an array of digits,

这是给定的问题:给定一个表示为数字数组的非负数,

add 1 to the number ( increment the number represented by the digits ).

给数字加 1(增加由数字表示的数字)。

The digits are stored such that the most significant digit is at the head of the list.

存储这些数字时,最高有效数字位于列表的开头。

Example:

例子:

If the vector has [1, 2, 3]

如果向量有 [1, 2, 3]

the returned vector should be [1, 2, 4]

返回的向量应该是 [1, 2, 4]

as 123 + 1 = 124.

如 123 + 1 = 124。

This is my code:

这是我的代码:

 public class Solution {
    public ArrayList<Integer> plusOne(ArrayList<Integer> A) {       
        int carry = 1;
        int length = A.size();
        ArrayList result = new ArrayList();

        for( int i = length - 1; i >=0; i-- ){
            int val = A.get(i) + carry;
            result.add(0,val % 10);
            carry = val / 10;
        }

        if (carry == 1){
            result.add(0,1);
        }

        for (int j = 0; j < result.size(); j++){
            if(result.get(j).equals(0))
                result.remove(j);
            else
                break;
       }

        return result;

    }
  }

However, in the test case: A : [ 0, 6, 0, 6, 4, 8, 8, 1 ]

但是,在测试用例中: A : [ 0, 6, 0, 6, 4, 8, 8, 1 ]

it says my function returns

它说我的函数返回

6 6 4 8 8 2

6 6 4 8 8 2

while the correct answer is

而正确答案是

6 0 6 4 8 8 2

6 0 6 4 8 8 2

I have no idea what is wrong with my code.

我不知道我的代码有什么问题。

Thanks!

谢谢!

采纳答案by Patrick Roberts

if(result.get(j).equals(0))
    result.remove(j);
else
    break;

This will fail if every other index contains a 0. Here's what happens:

如果所有其他索引都包含 0,这将失败。这是发生的事情:

0 6 0 6 4 8 8 2
^ (j = 0)

The 0 will be removed, and jis incremented by one.

0 将被删除,并j增加 1。

6 0 6 4 8 8 2
  ^ (j = 1)

Then this 0 is removed as well, skipping the first 6 in your array. To fix this, change the snippet to:

然后这个 0 也被删除,跳过数组中的前 6 个。要解决此问题,请将代码段更改为:

if(result.get(j).equals(0))
    result.remove(j--);
else
    break;

This compensates for when an index is removed so that jwill not skip the number immediately after any removed 0s.

这会在删除索引时进行补偿,以便j在任何删除的 0 之后不会立即跳过该数字。

回答by Nat

Check out a similar question at Looping through and arraylist and removing elements at specified index

查看循环和数组列表并删除指定索引处的元素中的类似问题

simpler to do just

做起来更简单

while (!result.isEmpty() && result.get(0).equals(0)) {
  result.remove(0);
}

This will keep removing the left most 0 until there is no more left most zero to be deleted.

这将继续删除最左边的 0,直到没有更多要删除的最左边的 0。

回答by Kushal

Your last forloop is removing 0from your result ArrayList<Integer>. After removing that loop, you will get perfect output

您的最后一个for循环正在0从您的结果中删除ArrayList<Integer>。删除该循环后,您将获得完美的输出

public static ArrayList<Integer> plusOne(ArrayList<Integer> A) {       
    int carry = 1;
    int length = A.size();
    ArrayList result = new ArrayList();

    for (int i = length - 1; i >= 0; i--) {
        int val = A.get(i) + carry; //2 8
        result.add(0, val % 10);    // 2 8
        carry = val / 10;  
    }

    if (carry == 1) {
        result.add(0, 1);
    }

//  for (int j = 0; j < result.size(); j++) {
//      if (result.get(j).equals(0))
//          result.remove(j);
//      else
//          break;
//  }

    for (boolean isZero = true; isZero; ) {
        isZero = result.get(0).equals(0);

        if(isZero)
            result.remove(0);
    }

    return result;
}