您看到的是,存储在内部是一个ArrayList<Object[]>。虽然您始终可以将Object 设置为任何其他对象,但如果您想阅读它,则需要显式转换:
Integer i1=1; // upper case Integer (not int), so it is an object
Object o1=i1; // setting Object to any object is okay
//Integer i2=o1; // this will not compile
Integer i2=(Integer)o1; // this will work fine
if(o1 instanceof Integer)
System.out.println("o1 is Integer: "+(Integer)o1*2);
if(o1 instanceof Double)
System.out.println("o1 is Double: "+(Double)o1*2);
System.out.println("passed the ifs, let's die");
Double d1=(Double)o1; // this will result in ClassCastException in runtime
(https://ideone.com/qdGtJP)
这就是为什么您无法看到/对request.get(0)[0] 的结果进行操作,因为那仍然是Object,而不是String 或CheckListRequest。它需要特定的转换(请注意,Java 中有一个 instanceof 运算符,因此您可以检查某些东西是否真的是您认为的那样 - 因为当它是其他东西时,转换尝试将会失败)
E-s 只是代表 ***E*** 元素,您可以在文档中的任何地方看到它,例如 https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/List.html:
模块 java.base
包 java.util
接口列表
类型参数:
E - 此列表中元素的类型
运行时,Java 不会真正跟踪您为容器类型指定要包含的内容,它会将任何内容放入 List 对象中,然后一脸吃惊地死去:
List<Integer> il=new ArrayList<>();
//il.add(new Object()); // this would not compile
List l=il;
l.add(new Object()); // this compiles, and runs, so add() does not complain
System.out.println("Length: "+il.size());
System.out.println(il.get(0)+3);
特别是ClassCastException,所以List<Integer>在内部知道它只是存储对象,只有编译器为我们生成一个方便的转换,所以我们可以假装List<Integer>有Integers里面。但事实并非如此。
旁注1:这就是List<int>不存在的原因:它需要对象
旁注2:对于字段,可以通过反射获取其“类型参数”(<E>)。
要重现屏幕截图上的类似结构:
public class Test {
public static class CheckListRequest {}
public static void main(String[] args) {
List<Object[]> array=new ArrayList<>();
array.add(new Object[] {new CheckListRequest(),"--"});
array.add(new Object[2]);
System.out.println("Test");
// would not compile, Object has no length()
//System.out.println(array.get(0)[1].length());
// compiles and runs just fine
System.out.println(((String)array.get(0)[1]).length());
}
}
例如,如果您在Test 行设置断点,您可以在调试器中看到非常相似的结构。