Reputation: 14980
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.
Upvotes: 2
Views: 378
Reputation: 159784
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
SimpleArrayListExample.java
to a new directory called ArrayList
.javac ArrayList/SimpleArrayListExample.java
java ArrayList.SimpleArrayListExample
Note package names typically use lowercase.
Upvotes: 3
Reputation: 11113
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) {
//...
}
Upvotes: 2
Reputation: 46408
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<>();
Upvotes: 2
Reputation: 29213
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).
Upvotes: 1
Reputation: 891
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/
Upvotes: 1
Reputation: 28687
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.
Upvotes: 2