This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList problem

 
Lars Jahr
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just started learning java, and I'm of course using the book Head First Java

This chapter's topic is ArrayLists but unfortunately I have run into some problems:

1. When I try to run the following code I get an error:

The error I get is:

ArrayList.java:3: type ArrayList does not take parameters
ArrayList<String> myList = new ArrayList<String>();
^
ArrayList.java:3: type ArrayList does not take parameters
ArrayList<String> myList = new ArrayList<String>();
^
2 errors

What am I doing wrong here? I've more or less copied the code out of the book.

2. I also get an error when trying to compile this file:


Error:
ArrayList2.java:6: cannot find symbol
symbol : method add(SimpleObject)
location: class ArrayList
myList.add(b);
^

Why doesn't t his work?

Can anyone give me a hint or two?
 
Henry Wong
author
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What am I doing wrong here? I've more or less copied the code out of the book.


The problem is the "more or less" part...

Basically, you named your class ArrayList, and then tried to use the ArrayList class. When compiling, the compiler is trying to use your ArrayList class, and not the one in the utils package.


The other issue is probably has the same cause. It is trying to use the ArrayList in the same package -- as I am guessing those two classes are in the same directory.

Henry
 
Lars Jahr
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, thank you! I'm really not used to java yet, so I just name my classes in the same way I would have done in python (I also forgot the import statement.. )

Well, your hint made my first problem disappear, however I still don't manage to use the add method.

I've rewritten my code to:


The error message I get now is:
Test2.java:10: cannot find symbol
symbol : method add(SimpleObject)
location: class java.util.ArrayList<java.lang.String>
myList.add(b);
^


Got another tip?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an ArrayList of String objects (that's what the <String> means). You can only add String objects to the ArrayList, and you are trying to add a SimpleObject - that won't work.

Try making your ArrayList an ArrayList of SimpleObjects:

ArrayList<SimpleObject> list = new ArrayList<SimpleObject>();
 
Lars Jahr
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hehe, it wasn't harder Thank you!
 
camilo lopes
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
generics is an appeal java 1.5.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic