Reputation: 49
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] + " ");
}
}
}
Upvotes: 2
Views: 6443
Reputation: 20820
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.
Upvotes: 1
Reputation: 32014
Notice package of class: Your ArrayList
is not java.util.ArrayList
.
Correct:
java.util.ArrayList b = new java.util.ArrayList();
Upvotes: 2
Reputation:
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>();
Upvotes: 0