• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Array-list allows to add different data types

 
yogesh doshi
Ranch Hand
Posts: 33
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

]
It shows some compile time warning why... I am using Java 8
List.png
[Thumbnail for List.png]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yogesh,
You are getting the compiler error because you aren't using generics. An ArrayList without <> is actually a raw ArrayList, not a generic array list.
 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting the compiler error because you aren't using generics


I think it is not compiler error. It may be just warning by the compiler.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narayana Bojja wrote:
You are getting the compiler error because you aren't using generics


I think it is not compiler error. It may be just warning by the compiler.

Sorry, yes. I meant compiler warning.
 
yogesh doshi
Ranch Hand
Posts: 33
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still below code is not working

 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your generic ArrayList declaration should be as follows to allow different data types.

Please observe the above generic ArrayList declaration. There is Object as a data type. So you can add any type of object to ArrayList.
If you want to add specific type of objects, you need to specify that type in the ArrayList declaration. Empty sharp brackets at left hand side are not allowed.
Hope this helps !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic