aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ArrayList" Watch "ArrayList" New topic
Author

ArrayList

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


has List l=new ArrayList(); been totally removed...nothing seems to be working with it?
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Without the generic parameters from the compiler's point of view the list will look as follows:



List.add() will accept an Object as parameter.

Floating point literals are doubles by default, and the compiler can box it into a Double, which is a descendant of Object that the add method accepts.

With the Integer generic parameter, however, the parameter of List.add() is an Integer, and there is no rule in the language for converting a double or Double to Integer when matching parameter types, so the compiler won't accept it.


OCJP 6 (93%)
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
whether it is a double or an int there is a compile error...
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

Hi Ankur

I ran below code

It compiled fine and gave me output as 2.

If change code to add generics part as shown in below code


here i get compile time error which is expected as we are trying to add double value in a list that takes only Integer
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method add(Integer) in the type List<Integer> is not applicable for the arguments (double)


Hope it clears

Check which Java version are you using to compile your code as Autoboxing feature was not present before Java 5 version. You might getting error because you may be running your code on lesser version of Java (Java 1.4,1.3 etc)


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


oh am sorry...actually when i compile the program, it compiles but gives a warning that program uses unchecked operations.....i thought this was a compile error but it is a warning because when i ran it i got the output 2

Thanks everyone for your efforts..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList
 
Similar Threads
Why Doesn't Java Have Operator Overloading?
Generics question
Regarding Arranging the elements in ArrayList
passing List to method that wants ArrayList
LinkedList problem