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.
have to leave something for the compiler to complain about, else it won't ever be happy
Yeah, mixed up the Vector and Map there, never post while the doorbell is ringing
Joined: Jan 25, 2006
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,
For instance Iterator<String> when iterating over an ArrayList<String>.
so is such a step no need?
Jeroen T Wenting
Joined: Apr 21, 2006
The call to get the Iterator from your ArrayList<String> will automatically return an Iterator<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);
Joined: Aug 03, 2002
What I meant about the Iterator is shown in the following
The declaration of the iterator that you get from the list needs to be declared with same type as the list elements if the cast is to be avoided. (For simplicity I'm not showing the loop constructs here)