Java,在多维数组中存储混合数据类型的最简单方法? [英] Java, easiest way to store mixed-data types in a multidimensional array?
问题描述
我有一个包含一些String和int的文件,我希望将它们存储在2D'array'中.最好的方法是什么?我有一段时间没有做Java了,我一直在使用VBA(那里没有数据类型),所以我有点生锈.
Ive got a file with some String and ints I wish to store in a 2D 'array'. What is the best way of doing this? I havent done Java for a while and i've been using VBA (where you have no datatypes), so i'm a little bit rusty.
推荐答案
如果需要的话,将其制成二维对象数组.
Make it a two dimensional array of Objects, if you must.
一个更好的解决方案是找到一个通用接口,并使其成为该接口的二维数组.
A better solution is to find a common interface, and make it a two dimensional array of that interface.
最好的解决方案是做类似的事情
The best solution is to do something like
public class Entry {
private String name;
private int value;
public Entry(String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {
return this.name;
}
public int getValue() {
return this.value;
}
}
并使其成为 Entry
s的一维数组.请注意,如果您真的想追求成功",请将上述 Entry
类重命名为实际上有意义的类名称.
And make it a single dimensional array of Entry
s. Note that if you really wanted to "go for the gold" rename the above Entry
class to a class name that actually makes sense.
这篇关于Java,在多维数组中存储混合数据类型的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!