开发者

cannot find symbol-method add (java.lang.integer)..whats the problem actually?

public class Arr开发者_如何学运维ayList
{
    // instance variables - replace the example below with your own
    public void processinput (String s)
    {
        int[] a = {34, 25, 16, 98, 77, 101, 24};

        ArrayList  b = new ArrayList();

        for(int i = 0; i < a.length; i++) {
            int d = a[i];
            if(d%2 > 0) {
               b.add(new Integer(d));
            }
        }

        for(int i = 0; i < b.size(); i++) {
            System.out.print(b.get(i) + " ");
        }

        for(int i = a.length; i >= 0; i--) {
            System.out.print(a[i] + " ");
        }
    }
}


Notice package of class: Your ArrayList is not java.util.ArrayList.

Correct:

java.util.ArrayList b = new java.util.ArrayList();


Problem lies in naming your class as ArrayList. In your ArrayList class you do not have a method defined as add(). You are invoking your package ArrayList not java.util.ArrayList.

Change the declaration to java.util.List<Integer> b = new java.util.ArrayList<Integer>();. Also for clarity rename your class to something meaningful.


I don't run code but at a glance I see that you start with a.length which will cause ArrayIndexOutOfBoundsException. fix this line like a.length - 1

for (int i = a.length - 1; i >= 0; i--) {
     System.out.print(a[i] + " ");
}

and this line

ArrayList<Integer>  b = new ArrayList<Integer>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜