0
// ArrayList
import java.io.*; 
import java.util.*;

public class ArrayList
{
    public static void main (String [] args)
    {
        Integer obj1 = new Integer (97);
        String obj2 = "Lama";
        CD obj3 = new CD("BlahBlah", "Justin Bieber", 25.0, 13);

        ArrayList objects = new ArrayList();

        objects.add(obj1);
        objects.add(obj2);
        objects.add(obj3);
    }
}

I am trying to create an array list with these three objects, why am I receiving this error?

ArrayList.java:15: cannot find symbol
symbol  : method add(java.lang.Integer)
location: class ArrayList
objects.add(obj1);
       ^
ArrayList.java:16: cannot find symbol
symbol  : method add(java.lang.String)
location: class ArrayList
objects.add(obj2);
       ^
ArrayList.java:17: cannot find symbol
symbol  : method add(CD)
location: class ArrayList
objects.add(obj3);
       ^
3 errors
3
  • 2
    Anything with Justin Bieber in it will fail. Commented Dec 5, 2013 at 6:20
  • 1
    You cannot used reserved keywords of Java as your class name or variable name. Commented Dec 5, 2013 at 6:22
  • Also, it usually is not necessary to import package.*; If you had not, maybe you would noticed the error by yourself. Commented Dec 5, 2013 at 6:22

6 Answers 6

4

Because, your class name is ArrayList. Change your class name like,

 public class ArrayListTest{
     .....
 }

Or, use fully qualified class name java.util.ArrayList

public class ArrayList{


public static void main (String [] args)
{
  ....
  java.util.ArrayList objects = new java.util.ArrayList();
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

change your class name ArrayList is already defined in java

see below

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Comments

0

I suggest you to read some articles about name conventions in Java. Because what you are doing now is misleading. http://java.about.com/od/javasyntax/a/nameconventions.htm

Your class name is not eligible and misunderstanding. You need to use more specific names and avoid using restricted names like collections.

2 Comments

why are you repeating the answers?
I am not repeating, I am suggesting to read some sources. I do not replay any answer to his question to solve the problem as everyone do. It is just misleading to create class with name like this. So it seems that he does not know name conventions
0

Since your class name is same as one of the Java API, you would need to use complete namesapce(with pacakge name) when you want to operate on Java API

java.util.ArrayList objects

Ideally, user defined classes should not named as Java System/API class names

Comments

0

You are not able to add the object because of line :

ArrayList objects = new ArrayList();

Always use generic type when using multiple objects like Integer, String......

Replace your ArrayList declaration with :

  ArrayList <Object> objects = new ArrayList <Object> ();

Object can take Integer and String (because both are subclass of Object class)

one more point here : you can use ArrayList as class name then you must declare your java.util.ArrayList with qualified package name.....if using other then List like Set, Map then your can use ArrayList as a class name without any package declaration with Set or Map..... ideally we should not use any reserve word as a class name....

Thanks : KP

Comments

-1
public class Demo
{
    public static void main(String args[])
    {
        System.out.println("Hello World");
    }
}

1 Comment

Please, edit your answer to add an explanation. See how to answer.

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.