我有一个方法必须返回一个数组集合.(这是JUnit中的参数化测试.)实际上我只需要返回三个字符串,但它们需要在一个数组集合中.这是我的方法:
public static Collection<Object[]> browserList() {
String[] firefox = { "firefox" };
String[] chrome = { "chrome" };
String[] ie = { "ie" };
ArrayList<String[]> list = new ArrayList<String[]>(3);
list.add(firefox);
list.add(chrome);
list.add(ie);
return list;
}
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误:Type mismatch: cannot convert from ArrayList<String[]> to Collection<Object[]>.
所以真的有两个问题:(a)这有什么问题,考虑到这ArrayList是一个实现Collection并String从中衍生而来Object; (b)我该如何解决?
谢谢你的帮助.
用一个例子解释问题:
public class DataWrapper<T> {
T data;
};
DataWrapper<Object> obj1 = new DataWrapper<Object>();
List<DataWrapper<?>> anyDataList = Arrays.asList(obj1); //this doesn't work
DataWrapper<Integer> objInt = new DataWrapper<Integer>();
anyDataList = Arrays.asList(obj1, objInt); //this work
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么"Arrays.asList(obj1)"不起作用?
我有一个Animal.Class和Dog类,它扩展了Animal.Class
我可以知道是否有一种快速简便的方法吗?
List<Dog> dogList = getAnimalList();
public List<Animal> getAnimalList(){
List<Animal> animalList = new LinkedList<Animal>();
return animalList;
}
Run Code Online (Sandbox Code Playgroud)
除非绝对必要,否则我不希望再看整个动物名单.
dog类只包含一个额外的布尔值以用于其他检查目的.
我正试图解决我遇到的这个继承问题。这是我目前的情况:

所以我有一个计划,可以是活动列表(类Activities)或要做的事情列表(类Todos)。
但是,如果我这样做Todos:
private List<Todo> todos;
public List<Activity> getPlanning(){
return todos;
}
Run Code Online (Sandbox Code Playgroud)
它说类型不兼容。
由于Todoextends from Activity,我们是否确定Todo至少提供与 相同的功能Activity?
也许更重要的是:
(我不是 UML 专家,所以如果我的图表中有一些错误,请原谅我)
我有一个名为FamilyHistoryPersonModel扩展名为的类的类PersonModel.
MapValue mapValue = new MapValue("relatives",
new GenericType<FamilyHistoryPersonModel>() {},
new GenericType<List<FamilyHistoryPersonModel>>() {} //error here
);
Run Code Online (Sandbox Code Playgroud)
构造函数:
private MapValue(String pluralUrl, GenericType<? extends PersonModel> singleResultGenericType, GenericType<List<? extends PersonModel>> listResultGenericType) {
this.pluralUrl = pluralUrl;
this.singleResultGenericType = singleResultGenericType;
this.listResultGenericType = listResultGenericType;
}
Run Code Online (Sandbox Code Playgroud)
错误:
cannot find symbol
symbol : constructor MapValue(java.lang.String,<anonymous com.sun.jersey.api.client.GenericType<com.models.FamilyHistoryPersonModel>>,<anonymous com.sun.jersey.api.client.GenericType<java.util.List<com.models.FamilyHistoryPersonModel>>>)
location: class com.rest.v2.resource.CreatePersonModelIntegrationTest.MapValue
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?我如何解决它?我不想改变构造函数来接受GenericType<List<FamilyHistoryPersonModel>> listResultGenericType
我使用的是JDK 1.6
我在Java 7,我有以下枚举:
public enum BooleanEnum implements StringRepresentable{
YES {
@Override
public String getStringRepresentation() {
return "true";
}
},
NO {
@Override
public String getStringRepresentation() {
return "false";
}
};
public abstract String getStringRepresentation();
}
Run Code Online (Sandbox Code Playgroud)
现在我有了方法:
List<StringRepresentable> getValues(){
return Arrays.asList(BooleanEnum.values()); //Type mismatch:
//cannot convert from List<BooleanEnum> to List<StringRepresentable>
}
Run Code Online (Sandbox Code Playgroud)
这有什么问题enum?它实现了接口,因此代码应该编译得很好.
public static void main(String... args) {
List<Child> list1 = new ArrayList<Child>();
method2(list1);
}
public static void method2(List<Parent> list1) {
}
Run Code Online (Sandbox Code Playgroud)
我得到以下编译错误
方法method2(List)未定义...
上面的问题可以通过修改List<Parent> list1来解决List<? extends Parent> list1.
但是如果我尝试添加下面的子对象
public static void method2(List<? extends Parent> list1) {
Child child1 = new Child();
list1.add(child1);
}
Run Code Online (Sandbox Code Playgroud)
它再次出现编译错误
类型List中的方法add(capture#1-of?extends Parent)不适用于参数(Child)
所以我的问题是,如果List<Child>可以作为参数传递给List<? extends Parent> list1我们为什么不能添加子对象List<? extends Parent>?
我希望有一个函数(例如)在两种情况下都输出Map的所有值:
Map<String, String> map1 = new HashMap<String, String>();
Map<String, Integer> map2 = new HashMap<String, Integer>();
output(map1, "1234");
output(map2, "4321");
Run Code Online (Sandbox Code Playgroud)
以下似乎不起作用:
public void output(Map<String, Object> map, String key) {
System.out.println(map.get(key).toString());
}
Run Code Online (Sandbox Code Playgroud)
两者不String和Integer型的Object?
我真的不知道如何解释我的问题.下面是我拥有的一些对象的示例.
让我们说我上课了
public class Foo{}
public class FooBar extends Foo{}
Run Code Online (Sandbox Code Playgroud)
所有这一切都很好,但是如果我拿一个列表说我希望它充满了Foo对象,我就不能提供它FooBar.为什么是这样?
例:
List<Foo> fooList = Arrays.asList(new FooBar());
Run Code Online (Sandbox Code Playgroud)
如果FooBar extends Foo,为什么它不能被抛回Foo?
在ExecutorService的开发过程中,有必要将List放入Set中.如何才能做到这一点?
public class Executor {
private Set<List<Future<Object>>> primeNumList = Collections.synchronizedSet(new TreeSet<>());
Set<List<Future<Object>>> getPrimeNumList() {
return primeNumList;
}
@SuppressWarnings("unchecked")
public void setup(int min, int max, int threadNum) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
List<Callable<Object>> callableList = new ArrayList<>();
for (int i = 0; i < threadNum; i++) {
callableList.add(new AdderImmediately(min + i, max, threadNum));
}
List<Future<Object>> a = executorService.invokeAll(callableList);
primeNumList.add(a); // here i try to add Future list into Set
System.out.println(primeNumList);
executorService.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
我的类我处理值并通过call()返回它们.之后,他们会从我希望将它们放入最终Set中的列表中进入列表
public class AdderImmediately implements Callable { …Run Code Online (Sandbox Code Playgroud)