你试图做的不是惯用的。相反,创建一个包含 3 个整数和 1 个字符串的类,然后将此类添加到您的列表中。
做你所要求的唯一方法是不要将泛型放在列表中,如下所示:
package com.sandbox;
import java.util.ArrayList;
import java.util.List;
public class Sandbox {
public static void main(String[] args) {
List rawInput = new ArrayList();
rawInput.add(1);
rawInput.add(2);
rawInput.add(3);
rawInput.add("s");
}
}
而且,虽然这可以编译,但它很糟糕,并给你警告,这是有充分理由的。相反,创建一个这样的类:
package com.sandbox;
import java.util.ArrayList;
import java.util.List;
public class Sandbox {
public static void main(String[] args) {
List<Character> rawInput = new ArrayList<Character>();
rawInput.add(new Character("Foo", new Location(1, 2, 3)));
}
public static class Character {
private String name;
private Location location;
public Character(String name, Location location) {
this.name = name;
this.location = location;
}
}
public static class Location {
private int x;
private int y;
private int z;
public Location(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
}