• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unsafe or Unchecked Operations with class that extends ArrayList

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am aware that if you are declaring ArrayList directly, you need to use a sort of typcasting in the declaration in order to avoid compiler warnings for statements that add to the ArrayList.

for example ArrayList<ObjectType> list1 = new ArrayList<ObjectType>();

however, I currently have the following class, and I have another class called Move.



now, when I declare using a statement such as FList<Move> moveList = new FList<Move>(); I avoid the compiler warning about unsafe or unchecked operations, but I get a new error that says type FList does not take parameters

And I can't figure out how to code my FList class to avoid this new problem. Till now I've ignored the compiler warnings because they are just warnings, but I would like to clean this up. So if someone can advise me on how to get my FList class to accept parameters, I would be grateful.

thanks.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
seems that this works....





Tillux


 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thanks, that seems to solve the issues.

I find it interesting that <T> only appears in the class "declaration" and is not referenced anywhere else. It's my first exposure to this kind of thing, so I guess I have some research to do to find out how this works. One more thing for the todo list.

I'm assuming that If I attempt to add an object to the ArrayList that is not of type Move, that will generate some other kind of error, I'll have to try that out later.

This all seems like something that Sun has added to Java to prevent the programmer from making logic errors, as opposed to adding any new functionality.

regards.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:I find it interesting that <T> only appears in the class "declaration" and is not referenced anywhere else.


But it is referenced somewhere else - in the ArrayList<T>. And ArrayList uses T a lot.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:

Fred Hamilton wrote:I find it interesting that <T> only appears in the class "declaration" and is not referenced anywhere else.


But it is referenced somewhere else - in the ArrayList<T>. And ArrayList uses T a lot.



ok, noted. Makes sense. All the functionality that deals with this sort of type casting (for want of a better word) is inherited from ArrayList. I was thinking more about the contents of my FList class.

With me, it's always one question leads to another. I'm wondering if there is ever a situation where I am adding functionality to my FList class, where I need to take this <T> into account. I'm thinking that as long as the elements of the particular instance of FList are the same type, it should be a non-issue.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:... this sort of type casting (for want of a better word)


Generics.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote: With me, it's always one question leads to another. I'm wondering if there is ever a situation where I am adding functionality to my FList class, where I need to take this <T> into account.


Let's say that you want your undo method to return all of the undone elements:
I could have done the adding and removing using an indexed ListIterator, but that would miss out on a) the speed of direct indexing of ArrayList, and b) the much speedier bulk removal with removeRange compared to a one-by-one removal.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:

Fred Hamilton wrote: With me, it's always one question leads to another. I'm wondering if there is ever a situation where I am adding functionality to my FList class, where I need to take this <T> into account.


Let's say that you want your undo method to return all of the undone elements:
I could have done the adding and removing using an indexed ListIterator, but that would miss out on a) the speed of direct indexing of ArrayList, and b) the much speedier bulk removal with removeRange compared to a one-by-one removal.



Thanks Rob, I might just need that. Right now when I have an List of moves in a chess game, and I backtrack to an earlier position in order to try a different move sequence, I just discard that which I have backtracked over. But if I want to save all of my different move sequences for further analysis, then I will need your idea.
 
Marshal
Posts: 79225
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you had a look at the about generics? Or even looked at Angelika Langer's FAQ?
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Have you had a look at the about generics? Or even looked at Angelika Langer's FAQ?



Nah, I was just going by examples of code I found where ArrayList was not extended. Now that I know the terminology, I'll definitely look into it. That FAQ looks pretty good, thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic