• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList  RSS feed

 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone

Could you please assist me with some confirmation on my understanding of ArrayList:

If you create an ArrayList, must you specify the data type eg:


or seeing that you can insert anything besides primitives, is it possible to create an ArrayList without specifying the data type like this:
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that's possible the latter one is called the Raw type means you can put in any Object be it String,Integer,Number...
But the former one is the Generic type,if you don't know about it you can taken a very good lesson here→Generics.
Generics provide a  compile time type safety.
see this declaration
List<String> list = new ArrayList<String>();
you can only put in String object but if you try to put Integer,Double,Number,Object..then your program will complain for it during compile time.
another thing,if you will draw the items back from this list you will not loose it's type information i.e.,it will be String but with Raw type it will be Object.
Also the generics is deleted after the code compiles which means their is no any generic information available during Runtime.it's because Generics were introduced in version 1.5 and their are plenty of code written before it's introduction so in order to hold the backward compatibility the designers have took this step.
this might be over your head right now but you will figure out these things after going through the link i have provided.

After going through the Tutorial try to figure out why the first 2 declarations(above) are Raw Type.
Kind Regards,
Praveen
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your assistance.

I understand the concept, but why do I still get an compile error when trying to compile the following code.



error: warning:[unchecked] unchecked call to add(E) as a member of the raw type List
            list.add("Hello");

Could this have something to do with me writing the code in notepad++, or am I missing something here. I don't want to just ignore this as it might be something important I am missing.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK wait, i think i got it.

Basically they saying, don't use Raw types. and stick with using generics.

Is my understanding correct ?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will wrote:Could this have something to do with me writing the code in notepad++, or am I missing something here. I don't want to just ignore this as it might be something important I am missing.

Nope it doesn't have to do anything with notepad.
that is not an error here compiler is warning you that you are using a Raw type code like you did.after the generics were added in java it's recommended to use a generic type as it renders a safety to the code.let's have an example:

here in this code compiler will generate warning for using the raw type at line 2 and the unchecked cast at line 4 but this code will compile and fail during runtime by getting a ClassCastException there.but if you will use generics then you can avoid such fails during compile time as it will only allow you to add the mentioned type objects in it.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Dev wrote:OK wait, i think i got it.

Basically they saying, don't use Raw types. and stick with using generics.

Is my understanding correct ?

exactly!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . List<String> list = new ArrayList<String>();
You shou‍ld have said that is probably the best way to declare a List of Strings.
. . . Also the generics is deleted after the code compiles which means their is no any generic information available during Runtime. . . .
That is an implementation detail which is different from some other languages. Some languages retain such generic information at runtime.

I can remember they said that raw types would be prohibited in new code, but that features has (probably unfortunately) never been implemented.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW, as a side note, if you want a collection that can hold anything, it is probably better to specify <Object> as the generic, instead of using the raw type.

Henry
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you everyone. i have a clear understanding now of ArrayList
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!