Siegfried Heintze wrote:
(1) Is not List<Integer> completely redundant with List<? extends Integer> because whenever we specify a (non-primitive) type, we are liberty to use that type or a descendant of that type?
I think this is the Liskov substitution principle. So what is the advantage of List<? extends Integer>?
Siegfried Heintze wrote:
(2) And what about the difference between function parameters of List verses List<Object> verses List<?>? Are these all redundant too?
Siegfried Heintze wrote:
In other words:
(3b) What is Java doing when I type "for (Integer n: nums){ n = n + 1; }" and nums is a List<Integer>? Apparently it is making a deep copy because when I print out the original List in the main program (or in this case, test function), the original list is not modified. Is there a way to make this do a shallow copy so I modify the original and avoid creating a new list as the function value?
Siegfried Heintze wrote:
(1) Is not List<Integer> completely redundant with List<? extends Integer> because whenever we specify a (non-primitive) type, we are liberty to use that type or a descendant of that type?
I think this is the Liskov substitution principle. So what is the advantage of List<? extends Integer>?
Bounded wildcard and the paramaterized Type(no bounds),they both have their own use,it completely depends upon the context for which is suitable.for ex-In List<Number> you can add a Number to it and you are sure that you will get a Number from it.Now lets take a another List with bounded wild card List<? extends Number> lets analyse the possibility for unknown(?),it is called capturing the types,the posibilities for this List can be:Seigfried wrote: (1) Is not List<Integer> completely redundant with List<? extends Integer> because whenever we specify a (non-primitive) type, we are liberty to use that type or a descendant of that type?
I think this is the Liskov substitution principle. So what is the advantage of List<? extends Integer>?
List is a raw type,you can add an instance of any type to it but if you will retrieve from it then you have a type-Object.Seigfried wrote:(2) And what about the difference between function parameters of List verses List<Object> verses List<?>? Are these all redundant too?
Their are many List implementation in the Java Collection framework.efficient modification will depend on the context which means what kind of operations are performed on the list(so that you can choose the best implementation of list having good time complexity for the algorithm).their are plenty of threads like *LinkedList vs ArrayList* in this forum like:Seigfried wrote:(3a) And lastly, is it possible to efficiently modify the elements of an existing list instead of creating and returning a new list? While I suppose you could call get and set for a function parameter of type List, I'm worried these would be very inefficient for large linked lists. C++, for example, has a special for-each-loop syntax so you receive a reference to the linked list element instead of a copy.
though,your code passes a six test but they will fail in certain circumstances.Seigfried wrote:Here is my code that passes all 6 tests
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Don't call subtypes children or descendants because biological inheritance and money inheritance are different from inheritance of classes.Siegfried Heintze wrote:. . . (1) Is not List<Integer> completely redundant with List<? extends Integer> because whenever we specify a (non-primitive) type, we are liberty to use that type or a descendant of that type?
I think this is the Liskov substitution principle. So what is the advantage of List<? extends Integer>?
No. For the same reason you can add things to a List<Object> and get them back but I don't think you can add things to a List<?>(2) And what about the difference between function parameters of List verses List<Object> verses List<?>? Are these all redundant too?
You can modify all mutable reference types if you can obtain a reference to the object. It is therefore possible to iterate a List and call methods to change the state of any or all its elements.(3a) And lastly, is it possible to efficiently modify the elements of an existing list instead of creating and returning a new list?
C++ is different language, completely unrelated to Java®. The apparent similarities encouraged C++ programmers to try Java® in its early days, but I think nowadays they cause more confusion than help.. . . C++, for example . . .
I think you have misunderstood the for‑each loop in Java® (officially called the enhanced for statement). You will see a version of a for‑each loop in the peach rectangles with orange border not surrounded by pale blue, with an explanation in the JLS's inimitable incomprehensible style. What it means is that a copy is taken of each element in your List. You can call methods on that copy to change the state of the element. Again you have chosen the wrong type because Integer is immutable. Had you chosen a mutable type, you would have been able to change its state with a method, e.g. myNumber.increment(1). But none of the subtypes of java.lang.Number has such an increment method, so what you are doing is not calling a method of the List's element at all. You are unboxing it and doing arithmetic and re‑assigning the result to the (boxed) copy in the loop. Since you are only affecting the loop copy, the List remains unchanged. Remember that the for‑each loop uses the List's Iterator behind the scenes, and Iterators do not allow structural change to their parent Collection by any other means, so you must regard a for‑each loop as presenting you with a read‑only view of the List. There is no copying of the List itself, whether deep or shallow.(3b) What is Java doing when I type "for (Integer n: nums){ n = n + 1; }" and nums is a List<Integer>? Apparently it is making a deep copy . . .
Lines 6‑12: You are taking elements out of a List<Integer> doing arithmetic with the copy reference and adding that copy to a new List<Integer>. Easy to explain.Siegfried Heintze wrote: . . . Here is my code that passes all 6 tests:
Don't get me started about those stupid light bulbs. |