ArrayList.size()不起作用

时间:2018-05-08 05:38:31

标签: java if-statement arraylist

我正在练习google kick start。我在练习中发现了这个问题https://code.google.com/codejam/contest/9234486/dashboard。 我到目前为止制作了以下代码,但它无效。

import java.util.*;
public class Solution {
    public static void main(String[] args) {
        int numOfCases = 0, numOfSteps = 0, num = 0;
        ArrayList<Integer> indexOfOdd = new ArrayList<Integer>();
        String input = "";
        boolean isOdd = false;
        Scanner in = new Scanner(System.in);
        numOfCases = in.nextInt();
        for (int i = 1 ; i <= numOfCases ; i++) {
            input = in.next();
            for(int j = 0; j<input.length(); j++) {
                num=Integer.parseInt(input.substring(j,j+1));
                if (num%2!=0) {
                    indexOfOdd.add(j);
                }
                System.out.println(indexOfOdd);
            }
            if(indexOfOdd.size() == 0) {
                numOfSteps = 1;
            }
            indexOfOdd.clear();
            System.out.println("Case #" + i + ": " + numOfSteps);
            }
    }
}

这是我的输出文件:

[]
[]
Case #1: 1
[0]
[0, 1]
Case #2: 1
[0]
Case #3: 1
[]
[]
[2]
[2]
Case #4: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
Case #5: 1
[]
Case #6: 1
[0]
[0, 1]
[0, 1]
[0, 1, 3]
[0, 1, 3, 4]
Case #7: 1
[]
[]
[]
[]
[4]
Case #8: 1
[0]
Case #9: 1
[]
[1]
[1]
[1]
[1]
Case #10: 1
[]
[1]
[1, 2]
[1, 2]
[1, 2, 4]
Case #11: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
Case #12: 1
[0]
[0]
[0]
[0]
[0]
Case #13: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
Case #14: 1
[]
[1]
[1, 2]
[1, 2]
[1, 2]
Case #15: 1
[]
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
Case #16: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #17: 1
[0]
[0]
[0]
[0, 3]
[0, 3]
Case #18: 1
[0]
[0]
[0, 2]
[0, 2]
[0, 2, 4]
Case #19: 1
[0]
[0]
[0, 2]
[0, 2, 3]
[0, 2, 3]
Case #20: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #21: 1
[]
[1]
[1]
[1, 3]
[1, 3]
Case #22: 1
[0]
[0]
[0]
[0]
[0]
Case #23: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1, 4]
Case #24: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2]
[0, 1, 2]
Case #25: 1
[]
[]
[2]
[2, 3]
[2, 3]
Case #26: 1
[]
[]
[]
[3]
[3, 4]
Case #27: 1
[]
Case #28: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
Case #29: 1
[]
[1]
[1, 2]
[1, 2]
[1, 2]
Case #30: 1
[0]
Case #31: 1
[0]
[0, 1]
[0, 1]
[0, 1, 3]
[0, 1, 3, 4]
Case #32: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
Case #33: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #34: 1
[0]
[0]
Case #35: 1
[0]
[0, 1]
Case #36: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #37: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #38: 1
[]
[]
[2]
[2]
[2, 4]
Case #39: 1
[0]
[0]
[0]
[0]
[0]
Case #40: 1
[]
[1]
[1]
[1]
Case #41: 1
[]
[]
[2]
[2]
[2]
Case #42: 1
[0]
[0]
[0, 2]
[0, 2]
[0, 2]
Case #43: 1
[]
[1]
[1, 2]
[1, 2]
[1, 2]
Case #44: 1
[]
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
Case #45: 1
[0]
[0]
[0]
[0]
[0]
Case #46: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1, 4]
Case #47: 1
[0]
[0]
[0, 2]
[0, 2, 3]
[0, 2, 3, 4]
Case #48: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
Case #49: 1
[]
Case #50: 1
[]
[1]
[1, 2]
[1, 2, 3]
Case #51: 1
[0]
[0]
[0]
[0]
[0, 4]
Case #52: 1
[0]
[0, 1]
Case #53: 1
[0]
Case #54: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2]
[0, 1, 2, 4]
Case #55: 1
[]
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
Case #56: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #57: 1
[0]
[0, 1]
[0, 1]
[0, 1, 3]
[0, 1, 3, 4]
Case #58: 1
[]
[1]
[1, 2]
[1, 2]
[1, 2, 4]
Case #59: 1
[]
[1]
[1]
[1, 3]
[1, 3, 4]
Case #60: 1
[0]
[0]
[0]
[0]
[0]
[0]
Case #61: 1
[]
[1]
[1]
[1, 3]
[1, 3]
Case #62: 1
[]
[1]
[1]
[1, 3]
[1, 3]
Case #63: 1
[0]
[0]
[0, 2]
[0, 2, 3]
[0, 2, 3, 4]
Case #64: 1
[0]
[0]
[0]
[0]
[0, 4]
Case #65: 1
[]
[]
[2]
[2]
[2, 4]
Case #66: 1
[]
[1]
[1]
[1]
[1]
Case #67: 1
[]
[]
[]
[]
[]
Case #68: 1
[]
[]
[2]
[2]
[2, 4]
Case #69: 1
[]
[1]
[1]
[1]
[1]
Case #70: 1
[]
[1]
[1]
[1]
[1, 4]
Case #71: 1
[0]
Case #72: 1
[0]
[0]
[0, 2]
[0, 2]
[0, 2, 4]
Case #73: 1
[]
[]
[2]
[2]
[2]
Case #74: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3]
Case #75: 1
[]
[1]
[1]
[1]
Case #76: 1
[]
[]
[]
[]
[]
Case #77: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2]
[0, 1, 2, 4]
Case #78: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2]
[0, 1, 2]
Case #79: 1
[0]
[0]
[0]
[0, 3]
[0, 3, 4]
Case #80: 1
[0]
[0]
[0, 2]
[0, 2]
[0, 2]
Case #81: 1
[0]
[0]
[0]
[0, 3]
Case #82: 1
[]
[]
[]
[]
[]
Case #83: 1
[0]
[0]
[0, 2]
[0, 2]
[0, 2]
Case #84: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #85: 1
[]
Case #86: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2]
[0, 1, 2, 4]
Case #87: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
Case #88: 1
[]
[]
[]
[]
[]
Case #89: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2]
[0, 1, 2, 4]
Case #90: 1
[]
[1]
[1, 2]
[1, 2]
[1, 2]
Case #91: 1
[]
[]
[2]
[2]
[2, 4]
Case #92: 1
[0]
[0, 1]
[0, 1]
[0, 1, 3]
[0, 1, 3, 4]
Case #93: 1
[0]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
Case #94: 1
[0]
[0]
[0]
[0]
Case #95: 1
[]
[]
[]
[]
[]
Case #96: 1
[]
[1]
[1, 2]
[1, 2]
[1, 2]
Case #97: 1
[]
[]
[]
[]
[4]
Case #98: 1
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2]
Case #99: 1
[0]
[0]
[0, 2]
[0, 2]
[0, 2, 4]
Case #100: 1

请帮帮我。我很困惑哪一部分不起作用。它应该像数组列表是空的一样工作,它应该显示1其他0.那么错误是什么????

我只是一名在印度读12年级的学生,所以我很欣赏所发现错误的详细说明。

https://drive.google.com/open?id=1fIVWsMXVJLknmlzkNE_2iQakW-nsMRXJ

您可以查看这些文件以供参考。 感谢。

2 个答案:

答案 0 :(得分:1)

在某些情况下,您希望值numOfSteps为零,但由于您没有重置每个输入的值,因此一旦列表中的值为一次,则每个其他测试都不正确。您只需根据条件将变量设置为零:

if(indexOfOdd.size() == 0) {
    numOfSteps = 1;
} else {
    numOfSteps = 0;
}

或者使用三元:

numOfSteps = indexOfOdd.size() == 0 ? 1 : 0;

您还可以在每次迭代开始时将numOfSteps重置为零。

for (int i = 1 ; i <= numOfCases ; i++) {
    numOfSteps  = 0;
    input = in.next();
    ...

编辑:

您可能会对返回布尔值的List.isEmpty方法感兴趣。

numOfSteps = indexOfOdd.isEmpty() ? 0 : 1;

答案 1 :(得分:0)

您需要添加

)

而不是

numOfSteps = indexOfOdd.size();

您修改后的方法如下。

if(indexOfOdd.size() == 0) {
  numOfSteps = 1;
}