user2990394
user2990394

Reputation: 55

Java Array List Error?

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

Upvotes: 0

Views: 6467

Answers (6)

user15129109
user15129109

Reputation: 1

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

Upvotes: -1

KP_JavaDev
KP_JavaDev

Reputation: 222

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

Upvotes: 0

Satheesh Cheveri
Satheesh Cheveri

Reputation: 3679

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

Upvotes: 0

Masudul
Masudul

Reputation: 21961

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();
  ...
}

Upvotes: 4

solvator
solvator

Reputation: 371

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.

Upvotes: 0

Deepak
Deepak

Reputation: 2895

change your class name ArrayList is already defined in java

see below

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

Upvotes: 0

Related Questions