This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
java_faq [2016/09/22 11:43] 178.237.8.52 [ArrayList] |
java_faq [2017/01/02 14:01] 178.237.8.52 [ArrayList] |
||
---|---|---|---|
Line 200: | Line 200: | ||
[edo, def, ghi] | [edo, def, ghi] | ||
- | Great! However we don't have the flexibility we wanted because the asList method [https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...- Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array]. So if one tries to add an element to the list the compiler complains ''uses unchecked of unsafe operations'' and upon execution it throws a java.lang.UnsupportedOperationException | + | Great! However we don't have the flexibility we wanted because the asList method [[https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...- | Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array]]. So if one tries to add an element to the list the compiler complains ''uses unchecked of unsafe operations'' and upon execution it throws a java.lang.UnsupportedOperationException |
- | Not great!!! We need an ArrayList that is a [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html Resizable-array implementation of the List interface.] | + | Not great!!! We need an ArrayList that is a [[https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html | Resizable-array implementation of the List interface.]] |
We cannot create the ArrayList from the String[] because the constructor of the ArrayList takes only int or Collections as parameters. | We cannot create the ArrayList from the String[] because the constructor of the ArrayList takes only int or Collections as parameters. | ||
Line 273: | Line 273: | ||
list2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | list2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
- | that means that ''list1'' is a list which contains an int[], while the other is what we expected to find. This has to do with [[https://docs.oracle.com/javase/tutorial/java/generics/types.html | Java generics]]. Java generics is the analogous of ''templates'' in C++, but while in C++ anything can be a ''typename'' in Java ''a type variable can be any NON-PRIMITIVE type''. | + | that means that ''list1'' is a list which contains an int[], while the other is what we expected to find. This has to do with [[https://docs.oracle.com/javase/tutorial/java/generics/types.html | Java generics]]. [[Java generics]] is the analogous of ''templates'' in C++, but while in C++ anything can be a ''typename'' in Java ''a type variable can be any NON-PRIMITIVE type''. |
In our example I used an array of primitives, which is the only object in the matter and Java used it to perform the transformation to List, as it is a non-primitive object. Therefore ''list1'' is a List containing one element of type ''int[]''. | In our example I used an array of primitives, which is the only object in the matter and Java used it to perform the transformation to List, as it is a non-primitive object. Therefore ''list1'' is a List containing one element of type ''int[]''. | ||