I want to declare an ArrayList
with multiple types, to be more clear to do as the following :
public ArrayList<Integer, String, Boolean> a = new ArrayList<Integer, String, Boolean>();
I know that example will not work because ArrayList
can be parameterized with only one type argument.
The HashMap
can allow me to use two arguments, but that's not enough in my case.
I know that I can declare a class with multiple attributes and use it as a parameter in my ArrayList
, but I don't want this method.
Isn't there any method to declare a List
with multiple types, or to use some internal class
to do that?
You can create it like this
public class Tuple<A, B, C> {
public A First;
public B Second;
public C Third;
}
and you declare it like this
List< Tuple <Integer, String, Bool> listOfTuple;
or check this out javatuples http://www.javatuples.org for Tuples from 1-10 Params
Guava中的Table结构似乎很好地满足了您的所有需求。
You can use polymorphism, create an interface lets say ListElement. All the objects you want to add to this list must implement this interface. Then just use an ArrayList of ListElement
The other possibility (since you are using classes like Integer) you can use a ArrayList of Object, since all objects extend the class Object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.