Aeternus Posted July 27, 2006 Posted July 27, 2006 http://butunclebob.com/ArticleS.UncleBob.BoundedWildcards That's quite a nice entry on how you can use wildcards although it still doesn't go indepth into some bits that would be nice.
albertlee Posted July 28, 2006 Author Posted July 28, 2006 thanks Aeternus for your explanation.... But regarding your explanation on question 2: The reason it works with super is that if the list stores any class/type that is a super type of the class specified then an object of the class you specified can be stored in any of those lists because it is extended from that class. I still dont understand why this works. I can possibly parameterize List<? super Integer> with ArrayList<Number>, and then try to add elements of Object type. Clearly, this should fail, and why not the compiler show an error while the extends keyword, if used, does show an error? For example, this wont work as well, List<? extends Object> l = new ArrayList<Integer>(); l.add(new Number()); (1) l.add(new Integer()); (2) and as expected, both cases will fail... but with super keyword, only case (1) fails but case (2) will be OK.... this is what I mean.... please help thx
albertlee Posted July 28, 2006 Author Posted July 28, 2006 Question 6: Given the fully-qualified class names: com.foo.bar.Dog com.foo.bar.blatz.Book com.bar.Car com.bar.blatz.Sun Which graph represents the correct directory structure for a JAR file from which those classes can be used by the compiler and JVM? What does the question mean??????? please help
Aeternus Posted July 28, 2006 Posted July 28, 2006 thanks Aeternus for your explanation.... But regarding your explanation on question 2: I still dont understand why this works. I can possibly parameterize List<? super Integer> with ArrayList<Number>' date=' and then try to add elements of Object type. Clearly, this should fail, and why not the compiler show an error while the [b']extends[/b] keyword, if used, does show an error? For example, this wont work as well, List<? extends Object> l = new ArrayList<Integer>(); l.add(new Number()); (1) l.add(new Integer()); (2) and as expected, both cases will fail... but with super keyword, only case (1) fails but case (2) will be OK.... this is what I mean.... please help thx Perhaps this - http://butunclebob.com/ArticleS.UncleBob.BoundedWildcards - will be of use to you. The point is in the case of super, you will be able to add anything to that list that is Integer or derived from Integer (which may sound confusing given the definition). The point is, it doesn't mean that this list can store anything that is a super of Integer, it means this variable can store a list of anything that is a super of Integer, there is a difference. This works because for instance - List<Object> l = new ArrayList<Object>(); l.add(new Integer()); would work because an Integer can be stored as an Object because it is derived from it. For this same reason, we know that anything that is a super of a particular type can store an object of that type, so in compiler checking the compiler will make sure that anything being passed into the object is an Integer or derived from an Integer, because those are the only thing is can guarantee can be passed to it For instance, say we have 3 classes such that, A, B extends A, C extends B and then we have - List<? super C> l = new ArrayList<B>; l.add(new A()); doesn't work, simply because you can only add C or lower to it as it can only guarantee that those types can be added. Extends has the opposite problem, by accepting any list that can contain objects of a type that extends the given type, you don't really know at all what type of objects you can store in it because the class could be anywhere down the class hierarchy and anything above that wouldn't be storable in the list. Sorry if this seems a little unclear. The blog/article (not sure if I've already posted it) might help clear things up.
albertlee Posted July 29, 2006 Author Posted July 29, 2006 Perhaps this - http://butunclebob.com/ArticleS.Uncl...undedWildcards - will be of use to you. Aeternus, I think the link you have provided contains a mistake or something at least. "The compiler complains that you cannot pass a List<Square> to an argument that expects a List<Shape>......" The above quote is very wrong.... How cant you add a List<Square> to an argument that expects a List<String>?????
albertlee Posted July 29, 2006 Author Posted July 29, 2006 Ok... please help me with question 6....Aeternus
Aeternus Posted July 29, 2006 Posted July 29, 2006 Aeternus' date=' I think the link you have provided contains a mistake or something at least. "The compiler complains that you cannot pass a List<Square> to an argument that expects a List<Shape>......" The above quote is very wrong.... How cant you add a List<Square> to an argument that expects a List<String>?????[/quote'] If you look at it in context, it is saying that you can't do - ... public Handler { public Handler(List<Shape> list) { this.list = list; } ... public void handleSquares() { List<Square> ls = new ArrayList<Square>(); /* get a bunch of squares */ Handler h = new Handler(ls); // <---Error h.handle(); } .... Ie, even though Square is derived from Shape, List<Square> can not be passed into something or typecast to something that expects List<Shape> which is the reason for the use of wildcards and so on, so that you can get the same functionality. This doesn't mean you can't add a square to a list of shapes, this means you can't store a list of squares in a variable that is intended to only hold a list of shapes, because even though a variable that holds a shape can hold a square, a list of shapes is not the same as a shape. With regard to question 6, it's something I've never really looked atm. The question seems to imply (it asks "which graph"??) that there are some graphs as well to go with the question? I'd say just try a couple out and see. I'll look into it later on and see if I can find out or play about with things to get the answer.
albertlee Posted July 29, 2006 Author Posted July 29, 2006 This is the funky bit, as k2.i has now changed to 1 (it was originally 2), k2.hashCode() will return 2, and HashSet will use that to look for it, but there is nothing at the position because that was the position taken by k1, k2 was originally placed into whatever place is specified by "2" not "1". Therefore as it can't find k2, it simply fails. k2 has not been removed, it is still at "2", it hasn't moved, it is simply that the hashCode() has changed and so HashSet tries to look for it in a different place. why??? this is the implementation of the hashCode method in my posted question: public int hashCode(){return i;} clearly, the method should return "1" not "2".....why would you see it's 2??? can you please explain more clearly why? please help thx
albertlee Posted July 29, 2006 Author Posted July 29, 2006 oh sorry... I think I need some sleep... I thought they were both "Square", but I actually overlooked. One of them is "Shape", not "Square".... Any way, please help answering my feedback questions on question 5.... thx
Aeternus Posted July 29, 2006 Posted July 29, 2006 why??? this is the implementation of the hashCode method in my posted question: public int hashCode(){return i;} clearly' date=' the method should return "1" not "2".....why would you see it's 2??? can you please explain more clearly why? please help thx[/quote'] Heh, sorry, that was a typo and should have been "1". The rest of it doesn't change (ie I simply typed 2 but meant to type 1 in that instance). Hope it makes more sense now. You can see this from the fact it says "but there is nothing at the position because that was the position taken by k1" which would mean that it would have to be "2".
albertlee Posted July 30, 2006 Author Posted July 30, 2006 Heh, sorry, that was a typo and should have been "1". The rest of it doesn't change (ie I simply typed 2 but meant to type 1 in that instance). Hope it makes more sense now. You can see this from the fact it says "but there is nothing at the position because that was the position taken by k1" which would mean that it would have to be "2". Ok, so... When will the hashcode of k2 really be changed to "1" in the HashSet?
Aeternus Posted July 30, 2006 Posted July 30, 2006 From the Set class - Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element. So try to avoid changing what the hashcode is once you've put things into a set. If you are going to change it, take it out and reinsert it? or perhaps if you are doing alot of operations on it, do the operations over the set and then create a new set (which will then remove any duplication). You might also be able to achieve the same behaviour with addAll() or you might be able to find a class that does something more akin to what you are looking for by looking for the things that implement the Set interface and trying them out or reading their docs. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now