Hi all I have read the net and notice this problem is caused by the version of compiler between 1.5 and 1.4. I tried to use the follow codes to overcome the warning msg. "uses unchecked or unsafe operations" but not able to. Please advice. Thanks. [code] public class XXX { private ArrayList <Customer> CustDB; ..... ..... public static void main(String[] args) { ...... ...... ...... FileInputStream readFile = new FileInputStream("cust.dat"); ObjectInputStream read = new ObjectInputStream(readFile); CustDB = (ArrayList<Customer>)read.readObject(); } } [/code] I tried to typecast the read object, but yet the warning still exists. May I know where have I did wrong?
"sunbin" <noSpam@noSpam.com> wrote in message news:eftt69$ivu$1@reader01.singnet.com.sg... > Hi all > > I have read the net and notice this problem is caused by the version of > compiler between 1.5 and 1.4. > > I tried to use the follow codes to overcome the warning msg. "uses > unchecked or unsafe operations" but not able to. Please advice. Thanks. > > [code] > > public class XXX > { > private ArrayList <Customer> CustDB; > ..... > ..... > public static void main(String[] args) > { > ...... > ...... > ...... > FileInputStream readFile = new FileInputStream("cust.dat"); > ObjectInputStream read = new ObjectInputStream(readFile); > > CustDB = (ArrayList<Customer>)read.readObject(); > } > } > > [/code] > > I tried to typecast the read object, but yet the warning still exists. > May I know where have I did wrong? Specifically, change the cast to: CustDB = (ArrayList<?>)read.readObject(); you'll probably have to add some more casts later on in your code in addition to making this change. In general, don't supply generic type arguments in cast expressions. - Oliver
"Oliver Wong" <owong@castortech.com> wrote in message news:l4PUg.12092$N4.4987@clgrps12... > Specifically, change the cast to: > > CustDB = (ArrayList<?>)read.readObject(); > > you'll probably have to add some more casts later on in your code in > addition to making this change. Thanks for the reply. I dont quite get what you mean. I am precisely doing what your post listed CustDB = (ArrayList<?>)read.readObject(); but the warning msg just won't get away. > In general, don't supply generic type arguments in cast expressions. Sorry I do not know what's generic, mind to explain?
"Tor Iver Wilhelmsen" <jadedgamer@hotmail.com> wrote in message news:uk63gx71q.fsf@hotmail.com... > Or add a @SuppressWarnings("unchecked") annotation to the method. Well how do I add that to my codes... sorry i cannot understand the @suppress thingy.
"Oliver Wong" <owong@castortech.com> writes: > Specifically, change the cast to: > > CustDB = (ArrayList<?>)read.readObject(); Or add a @SuppressWarnings("unchecked") annotation to the method.
"sunbin" <noSpam@noSpam.com> wrote in message news:eg0nsj$lgt$1@reader01.singnet.com.sg... > > "Oliver Wong" <owong@castortech.com> wrote in message > news:l4PUg.12092$N4.4987@clgrps12... > >> Specifically, change the cast to: >> >> CustDB = (ArrayList<?>)read.readObject(); >> >> you'll probably have to add some more casts later on in your code in >> addition to making this change. > > Thanks for the reply. > > I dont quite get what you mean. I am precisely doing what your post listed > > CustDB = (ArrayList<?>)read.readObject(); > > but the warning msg just won't get away. Are you sure? In your original post, you said you wrote: CustDB = (ArrayList<Customer>)read.readObject(); But I'm telling you to write: CustDB = (ArrayList<?>)read.readObject(); Notice that I don't pass "Customer" as a generic type argument. > >> In general, don't supply generic type arguments in cast expressions. > > Sorry I do not know what's generic, mind to explain? To put it informally, generic type arguments are the stuff that appear between the angle brackets in your code. If you don't understand how they work, it's probably best not to use them at all (remove everything between the angle brackets, and the angle brackets themselves), so the code would look like: CustDB = (ArrayList)read.readObject(); instead of CustDB = (ArrayList<Customer>)read.readObject(); You'll get warnings about not using generics, but you should just ignore them, since you don't know how to use generics yet. - Oliver
"sunbin" <noSpam@noSpam.com> writes: > Well how do I add that to my codes... sorry i cannot understand the > @suppress thingy. Annotations is another of the new JSE 5 features, like the generics you already use. http://www.oracle.com/technology/pub/articles/hunter_meta.html
sunbin wrote: > > I tried to use the follow codes to overcome the warning msg. "uses unchecked > or unsafe operations" but not able to. Please advice. Thanks. > ObjectInputStream read = new ObjectInputStream(readFile); > > CustDB = (ArrayList<Customer>)read.readObject(); Here's an answer I prepared earlier: http://groups.google.com/group/comp.lang.java.programmer/tree/browse_frm/thread/3c05ddbbe4eaaba4#doc_585daaa682e4e563 Tom Hawtin
"Tor Iver Wilhelmsen" <jadedgamer@hotmail.com> wrote in message news:usli3x502.fsf@hotmail.com... > "sunbin" <noSpam@noSpam.com> writes: > >> Well how do I add that to my codes... sorry i cannot understand the >> @suppress thingy. > > Annotations is another of the new JSE 5 features, like the generics > you already use. > > http://www.oracle.com/technology/pub/articles/hunter_meta.html Hey thanks. I got manage to suppress the warning message.