2

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!

1
  • 1
    Probably because it does not show any research effort ;) Commented Sep 29, 2011 at 8:24

7 Answers 7

6

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 + " ");
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm embarassed I used "ArrayList" as a class name. I changed that and my import statements just like your snippet and it's compiling great! thanks for the quick detailed response epoch!
3

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.

1 Comment

Changed my class name and import statements and it works, thanks for the quick response JB Nizet!
2

Well there are some strange things:

  1. First you are not importing java.util.ArrayList.
  2. You don't need to import java.lang.Iterable because classes from java.lang are imported automatically.
  3. Your class has the same name of ArrayList, and this is not a good idea. When you write "ArrayList hookSizesList = new ArrayList();" you are initializing a java.util.ArrayList but your own class, that is not a generic class. Try doing:

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 + " ");
}
}

1 Comment

Fabio, as a young kid, I appreciate your clear answers to my question. Without them, it would be a lot harder for kids like me to teach myself how to program. I wish I had reputation points to vote all these answers up!
1

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.

Comments

0

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.

Comments

0

The ArrayList class defined in your code (not java.util.ArrayList<E>) does not define any generic type parameter, so you cannot use new ArrayList<Integer>(), but only new ArrayList() (same for variable declaration).

1 Comment

That makes sense. As a noob, I appreciate your quick clarification michael! It's very encouraging!
0

You should use a different name for your class. Rename your class from ArrayList to something else.

Comments

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.