My Programmer Certification is 1.4, so I dont know the new features in JDK1.5. After I have finished most of my codes which compiled and run in JDK1.4, I tried to move to the JDK1.5 which is required by the assignment instruction. While I compile the Data.java[URLyBird], the computer says unchecked or unsafe implemention. Then I compile once again with appendix
"-Xlint:unchecked" there are total 3 warnings: java.util.HashMap 's member put(K, V)'s calling is unchecked; java.util.Vector 's member add(E )'s calling is unchecked [this appeared twice]
Could you tell me why?
Originally posted by Jeroen T Wenting:
You don't use genericised collections.
Your Vector stores something of class A with keys of class B.
Define it as Vector<B, A> rather than Vector.
I think Jeroen was in a hurry when posting this
I'm sure he meant use HashMap<A, B> rather than vector<A,B>. The vector definition would be vector<A> for some classes A.
So for example, use HashMap<String, Integer> map = new HashMap<String, Integer>(); or vector<String> myList = new Vector<String>();
you should find that you can get rid of some casts when you get elements out of such collections. If you are using Iterators you should declare them with the appropriate type. For instance Iterator<String> when iterating over an ArrayList<String>.
There's lots more but the above should help you get rid of a few of those warnings.
Thanks for your help. Now such warning is disappeared. I only rectified HashMap<Integer, String> and Vector<Integer> satisfiedRecNo. But Iterator which iterats satisfiedRecNo did no change,
so is such a step no need?
For instance Iterator<String> when iterating over an ArrayList<String>.
It would be cleaner (and probably get you a higher score) to do away with that Iterator and instead use the new forEach loop syntax:
Mind you won't have an index number here, but then when you iterate over an iterator you won't have one either.
And also remember you no longer need to cast the values retrieved from your Vector or Map to the type you defined it to be for.
So in the above example List you'd not need to do String t = (String) s.get(0); but you'd just do String t = s.get(0);