User Tools

Site Tools


java_faq

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
java_faq [2016/09/22 11:40]
178.237.8.52 [ArrayList]
java_faq [2016/09/22 11:44]
178.237.8.52 [ArrayList]
Line 163: Line 163:
 ===== ArrayList ===== ===== ArrayList =====
  
-Arrays are unflexible in the way that they cannot be resized once created. When you need more flexibility in this respect you use an ArrayList. An ArrayList implements the [https://​docs.oracle.com/​javase/​8/​docs/​api/​java/​util/​List.html List] interface.+Arrays are unflexible in the way that they cannot be resized once created. When you need more flexibility in this respect you use an ArrayList. An ArrayList implements the [[https://​docs.oracle.com/​javase/​8/​docs/​api/​java/​util/​List.html ​List]] interface.
  
 A list is  A list is 
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 270: Line 270:
  
 The output is kind of surprising: The output is kind of surprising:
- list1 [[I@15db9742] +  ​list1 [[I@15db9742] 
- 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''​. 
-[[http://​www.oracle.com/​technetwork/​java/​javase/​downloads/​jdk8-downloads-2133151.html | Oracle]] 
  
 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[]''​.
  
java_faq.txt · Last modified: 2017/01/02 14:01 by 178.237.8.52