2

I have download a simple java example from the net.I am trying to compile the code given below

package ArrayList;
import java.util.ArrayList;

public class SimpleArrayListExample {

    public static void main(String[] args) {

        //create an ArrayList object
        ArrayList<String> arrayList = new ArrayList<String>();

        /*
        Add elements to Arraylist using
        boolean add(Object o) method. It returns true as a general behavior
        of Collection.add method. The specified object is appended at the end
        of the ArrayList.
         */
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");

        /*
        Use get method of Java ArrayList class to display elements of ArrayList.
        Object get(int index) returns and element at the specified index in
        the ArrayList
         */
        System.out.println("Getting elements of ArrayList");
        System.out.println(arrayList.get(0));
        System.out.println(arrayList.get(1));
        System.out.println(arrayList.get(2));
    }
}

I have edited the program as per your suggestion and I can compile and I got the class file.

java SimpleArrayListExample.java

NOw I am trying to execute the file using

java -classpath . ArrayList.SimpleArrayListExample.java

Exception in thread "main" java.lang.NoClassDefFoundError

I google and found out that I have to specify -classpath .

http://www.tech-recipes.com/rx/826/java-exception-in-thread-main-javalangnoclassdeffounderror/ that doesn't seem to fix the problem.

liv2hak
  • 12,597
  • 41
  • 127
  • 231
  • Did you try what the compiler suggested, "Recompile with -Xlint:unchecked"? – Miserable Variable Jan 23 '13 at 21:42
  • (-1) *Please search for the error message before asking a question* - try `arraylist unchecked or unsafe operation` –  Jan 23 '13 at 21:42
  • http://stackoverflow.com/questions/12947806/unsafe-or-unchecked-operations-for-arraylist , http://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning –  Jan 23 '13 at 21:43
  • 2
    It should be `ArrayList arrayList` for the declaration. – FThompson Jan 23 '13 at 21:59

6 Answers6

3

The compiler is warning that the type contained within the collection has not been declared. You can use generics to emilinate this warning

ArrayList<String> arrayList = new ArrayList<String>();

Edit:

There is a problem with your package structure. The SimpleArrayListExample is not in a directory called ArrayList. To fix

  • Move SimpleArrayListExample.java to a new directory called ArrayList.
  • Compile using:- javac ArrayList/SimpleArrayListExample.java
  • To Run: java ArrayList.SimpleArrayListExample

Note package names typically use lowercase.

Reimeus
  • 152,723
  • 12
  • 195
  • 261
2

You can use generics to avoid this warning:

public static void main(String[] args) {

    //create an ArrayList object
    ArrayList<String> arrayList = new ArrayList<String>();

    //...
}

You could also suppress the warning if you don't want to use generics:

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    //...
}
John Ericksen
  • 10,783
  • 4
  • 43
  • 72
2

Restrict your collection by providing the generic type for your collections or in other words make your collection type safe(compile time only) and the warning will disappear

 ArrayList<String> arrayList = new ArrayList<String>();

also from Java 7 due to inroduction of Type Inference

 ArrayList<String> arrayList = new ArrayList<>();
PermGenError
  • 43,905
  • 8
  • 81
  • 104
2

That's not a compilation error; rather, it is a compilation warning. This means that it will still compile, but it is recommended that the warning be fixed.

In this case, it's because your ArrayList is lacking a generic type. You should declare it as an ArrayList<String> instead, since you're only adding strings to it.

FThompson
  • 27,043
  • 11
  • 53
  • 89
1

Make it an ArrayList<String>.

This is called a generic collection, which means it's a collection that can be parameterized with a type, to store and retrieve objects of that type.

So, if you specify you want it to store String, the compiler will know that it returns String too. If you don't specify anything, it essentially falls back to Object, which means it can store anything (non-scalar) and retrieve anything (which, in turn, makes the compiler jumpy).

Theodoros Chatzigiannakis
  • 26,988
  • 8
  • 61
  • 97
1

It's not an error, but a warning.

It's caused because you are using ArrayList without a generic type.

You may want to read more about generics: http://docs.oracle.com/javase/tutorial/java/generics/

Gothmog
  • 803
  • 1
  • 8
  • 19