This is a simple exercise from Chapter 2 of the Sun Certified Java Associate Study Guide that i'm having trouble with. When I try to compile the class below, I get the following error message:
"error: type ArrayList does not take parameters ArrayList hookSizesList = new ArrayList<>;"
import java.lang.Iterable;
public class ArrayList {
public static void main(String[] args) {
Integer hookSizeList;
ArrayList<Integer> hookSizesList = new ArrayList<Integer>();
hookSizesList.add(1);
hookSizesList.add(4);
hookSizesList.add(5);
for (Integer hook: hookSizesList) System.out.print(hook + " ");
}
}
I'd really appreciate any help in advance, thanks!
you have numerous errors, here is a updated snippet
import java.util.*;
public class Numbers {
public static void main(String[] args) {
List<Integer> hookSizesList = new ArrayList<Integer>();
hookSizesList.add(1);
hookSizesList.add(4);
hookSizesList.add(5);
for (Integer hook: hookSizesList) {
System.out.print(hook + " ");
}
}
}
You probably want to use the class java.util.ArrayList
. But since you named your own class ArrayList
, this is the class your program uses instead, and your ArrayList
class doesn't take any generic parameter. Change the name of your class and import java.util.ArrayList
.
Well there are some strange things:
import java.util.ArrayList;
public class MyArrayList {
public static void main(String[] args) {
Integer hookSizeList; //<--looks useless
ArrayList<Integer> hookSizesList = new ArrayList<Integer>();
hookSizesList.add(1);
hookSizesList.add(4);
hookSizesList.add(5);
for (Integer hook: hookSizesList) System.out.print(hook + " ");
}
}
The class you create is named ArrayList.
public class ArrayList {
The class the example is about is java.util.ArrayList.
Change the name of your class and import java.util.ArrayList.
Your class doesn't use generics so you can't parameterize ArrayList
with <Integer>
. Above that, it doesn't seem like you implemented the add()
method or implemented Iterable
or anything else for that matter. But to solve the generics problem, you would have to have a class declaration like: public class ArrayList<E>
Perhaps you were trying to import java.lang.ArrayList;
? In that case, you might want to consider renaming your class to something else.
您的代码中定义的ArrayList
类(不是java.util.ArrayList<E>
)不定义任何泛型类型参数,因此您不能使用new ArrayList<Integer>()
,而只能使用new ArrayList()
(对于变量声明也是如此) 。
You should use a different name for your class. Rename your class from ArrayList
to something else.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.