| Author |
ArrayList problem
|
Lars Jahr
Greenhorn
Joined: Dec 20, 2007
Posts: 3
|
|
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:
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
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Lars Jahr
Greenhorn
Joined: Dec 20, 2007
Posts: 3
|
|
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:
Got another tip?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12956
|
|
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>();
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Lars Jahr
Greenhorn
Joined: Dec 20, 2007
Posts: 3
|
|
Hehe, it wasn't harder Thank you!
|
 |
camilo lopes
Ranch Hand
Joined: Aug 08, 2007
Posts: 202
|
|
|
generics is an appeal java 1.5.
|
Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
|
 |
 |
|
|
subject: ArrayList problem
|
|
|