*
The moose likes Beginning Java and the fly likes Unsafe or Unchecked Operations with class that extends ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unsafe or Unchecked Operations with class that extends ArrayList" Watch "Unsafe or Unchecked Operations with class that extends ArrayList" New topic
Author

Unsafe or Unchecked Operations with class that extends ArrayList

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
Till Stoschus
Greenhorn

Joined: May 28, 2009
Posts: 14
seems that this works....





Tillux


Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

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

Generics.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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

Joined: May 13, 2009
Posts: 679
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Have you had a look at the about generics? Or even looked at Angelika Langer's FAQ?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Unsafe or Unchecked Operations with class that extends ArrayList