2
public class ArrayList
{
    // 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] + " ");
        }
    }
}
2
  • This code could use some work beyond just fixing the compiler error. You should probably be using ArrayList<Integer> rather than ArrayList, since that way you get all the benefits of type-checking. You don't need to explicitly wrap up the integer d with new Integer(d); autoboxing will do this for you. Finally, the code in the last for loop will immediately cause an out of bounds exception, since index a.length in array a will never succeed. You probably want to set i = a.length - 1. Commented Jan 26, 2011 at 5:52
  • Is there a reason you didn't simply edit your first question about this? Commented Jan 26, 2011 at 7:55

3 Answers 3

2

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

Correct:

java.util.ArrayList b = new java.util.ArrayList();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thanks for formatting Joachim. I wrote the answer from my iPad and unfortunately the formatting options do not show up on it.
0

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

1 Comment

it works for me. Change your className. and fix these line it will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.