• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My code compiled well in JDK1.4 but warning in JDK1.5.

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

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?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,

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
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a same dilemma, row or not in the interfaces ?
Example :

I have a interface :


and its implementation :



and its usage is :



Is the line :


correct or not ?

Regards,
Mihai
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be correct. List<Integer> is a valid datatype and indeed the exact type returned by the method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic