• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

ArrayList problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hehe, it wasn't harder Thank you!
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
generics is an appeal java 1.5.
 
I just had the craziest dream. This tiny ad was in it.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic