Unchecked or unsafe warning message

Hi,

I am getting the following warning message:

Note: ........ uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details

The following is the part of the class thats creating it, the line in bold is the line causing the warning :

public class MyPanel extends JPanel

{

//player field

private Player player;

//game rooms field

private ArrayList<Room> rooms;

/**

* Constructor for objects of class MyPanel

*/

public MyPanel(Player player, ArrayList rooms)

{

super();

setBackground(Color.GRAY);

setPreferredSize(new Dimension(240, 320));

this.player = player;

this.rooms = new ArrayList<Room>(rooms);

}

/**

* Paint the panel.

*/

public void paintComponent(Graphics g)

{

//other code

}

}

Please can anyone help.

Thanks!

[957 byte] By [HRahman10a] at [2007-11-27 0:58:09]
# 1
Look at that ArrayList constructor.public ArrayList(Collection<? extends E> c)You are calling it like this.public ArrayList(Collection c)
CaptainMorgan08a at 2007-7-11 23:31:56 > top of Java-index,Java Essentials,New To Java...
# 2

I though this was how it was meant to be. However, I have tried

this.rooms = new ArrayList(rooms);

but it is still showing the warning.

I have also seperated the line:

this.rooms = new ArrayList<Room>(rooms);

to:

this.rooms = new ArrayList<Room>();

this.rooms=rooms;

the warning is now associated with the second line. Could you please how I should code it.

thanks!

Message was edited by:

HRahman10

HRahman10a at 2007-7-11 23:31:56 > top of Java-index,Java Essentials,New To Java...
# 3
your constructor is still taking a raw ArrayList. parameterize that, too public MyPanel(Player player, List<Room> rooms)// etc
georgemca at 2007-7-11 23:31:56 > top of Java-index,Java Essentials,New To Java...
# 4
Brilliant!!!. That was bang on!!.Thanks so much!
HRahman10a at 2007-7-11 23:31:56 > top of Java-index,Java Essentials,New To Java...
# 5

> Brilliant!!!. That was bang on!!.

>

>

> Thanks so much!

you're welcome. notice that I also changed the reference in the constructor from ArrayList to just List. this is a good OOP principle known as "coding to an interface, not a concrete class". google that for more info, but it's a good habit to get into (you could possibly even use Collection<Room>, but one step at a time)

georgemca at 2007-7-11 23:31:56 > top of Java-index,Java Essentials,New To Java...
# 6

public MyPanel(Player player, List<? extends Room> rooms)

// etc

This code is less restrictive, increase the possibilities of the code. Using restricted wildcards you let pass as parameter a list of subclasses of a class, see how the JDK API use this, for example, look the addAll method of Collection:

http://java.sun.com/javase/6/docs/api/java/util/Collection.html#addAll(java.util.Collection)

This method guarantees that objects introduced are valids, but doesn't oblige to collection container be typed as Collection type. In addiction, constructors of ArrayList, LinkedList, etc, have a constructor of this form. You can use furthermore a declaration of Collection of Iterable, but it can be dangerous, because if in the future you want to share the reference passed as parameter and the property of the class, you can't do it. You must see what use will have the class more ahead

govisagod512a at 2007-7-11 23:31:56 > top of Java-index,Java Essentials,New To Java...