File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList problem" Watch "ArrayList problem" New topic
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:

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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:
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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 8 API documentation
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/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList problem