我需要一个可以存储和返回不同类型值的类。我无法定义我自己的班级。 我发现的唯一满足我需求的课程是List类。 我从数据库表中检索值。 这是我的方法:具有可变数量变量的类
//the total number of these variables can vary. The same is true for their types;
int intValue = 1;
String stringValue = "text 1";
byte[] dataValue = new byte[4];
List row = new ArrayList();
row.add(intValue);
row.add(stringValue);
row.add(dataValue);
List rows = new ArrayList();
rows.add(row);
intValue = 6;
stringValue = "text 2";
dataValue = new byte[8];
row = new ArrayList();
row.add(intValue);
row.add(stringValue);
row.add(dataValue);
rows.add(row);
String text1 = (String) ((List)rows.get(0)).get(1);
String text2 = (String) ((List)rows.get(1)).get(1);
您能否提供一个更好的方式来存储的通用类型的数据,因为我觉得我的实现是错误的。
“你能提出一个更好的方法来存储泛型类型” - 答案在你的问题。 – Maroun
在相同的数据结构中存储'Object'类型的变量不是通用的,而且大多数时候是不好的设计。 – Dici
但是铸件怎么样,是否耗费资源? – vovahost