This error appeared when I tried to use an ArrayList<ArrayList<Integer>>() new a List<List<Integer>> object
List<List<Integer>> = new ArrayList<ArrayList<Integer>>();
Maybe we will find that if the second ArrayList is changed to List, the error is gone, so what is the principle?
After searching, it is found that this is a common pit of generic applications:
Generics, Inheritance, and Subtypes
https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
1. First, if A is a B, we can assign A to B
Object someObject = new Object();
Integer someInteger = new Integer(10);
someObject = someInteger; // OK
Integer inherits from Object and is a subtype of it, so there is no problem in assigning values in this way. This is a generic type.
2. Integer is also a kind of Number, and Double is also a kind of Number, so the following is also possible
public void someMethod(Number n) { /* … */ }
someMethod(new Integer(10)); // OK
someMethod(new Double(10.1)); // OK
You can also use generics:
Box<Number> box = new Box<Number>();
box.add(new Integer(10)); // OK
box.add(new Double(10.1)); // OK
3. Here comes the point
public void boxTest(Box<Number> n) { /* … */ }
If so, can we pass in Box<Integer> or Box<Double>
the answer is negative.
Integer is a subclass of Number, and Double is also a subclass of Number. However, Box<Integer> and Box<Double> are not subclasses of Box<Integer>. Their relationship is parallel, and both are subclasses of Object.
Read More:
- Error: (list) object cannot be coerced to type ‘double’
- C++ new types may not be defined in a return type Error?
- [Solved] RuntimeError: each element in list of batch should be of equal size
- How to Solve “/usr/bin/ld: skipping incompatible”
- [Solved] Kubernetes Error: failed to list *core.Secret: unable to transform key
- /usr/bin/ld: skipping incompatible Error [How to Fix]
- [Solved] Installation failed due to: ‘INSTALL_FAILED_SHARED_USER_INCOMPATIBLE: Package com.
- An error was reported when Maven package was running the packaged jar package: there is no main list attribute in xxx.jar, which can be solved by configuring Maven plugin
- How to Solve Error: could not list the contents of folder
- Error: uncaughtexception: cannot find module ‘internal / util / types’
- [Solved] error [email protected]: The engine “node” is incompatible with this module.
- [Solved] MindSpore Error: Select GPU kernel op * fail! Incompatible data type
- [Solved] error: invalid operands of types ‘const char [6]‘ and ‘const char [6]‘ to binary ‘operator+‘
- Attributeerror encountered when using ceilometer and gnocchi to execute commands related to gnocchi metric list: _Environ instance has no attribute ‘
- C Language error: two or more data types in declaration specifiers
- Module build failed: Error: Node Sass version 6.0.0 is incompatible with ^4.0.0
- visual studio Error D8016 ‘/ZI’ and ‘/Gy-‘ command-line options are incompatible
- Error in Tensorflow using variables: List of Tensors when single Tensor expected
- [Solved] Neo4j Error: Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec
- [Solved] Yarn install Error: “error Found incompatible module.”