java 在java中制作混合二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17296571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a mixed 2D array in java
提问by tortilla
I want a 2D Matrix with one line of strings and the other line with int's. Is that possible? Or do I have to save the int's as strings and later convert them to int's again?
我想要一个二维矩阵,一行是字符串,另一行是整数。那可能吗?或者我是否必须将 int 保存为字符串,然后再将它们转换为 int?
回答by Ondra ?i?ka
Rather use an object.
而是使用一个对象。
class MyEntry {
String foo;
int number;
}
MyEntry[] array = new MyEntry[10];
But if you must, you can use two types - only through an Object
supertype.
但如果必须,您可以使用两种类型 - 只能通过Object
超类型。
Object[][] arr = new Object[2][10];
arr[0][0] = "Foo";
arr[1][0] = new Integer(50);
回答by NINCOMPOOP
No it is not possible . There can be only a single datatype for an array object. You can make a class having both the int
and String
as property and use it. Never use an Object[][]
even if there is a temptation to do so, it is an evil workaround and hacks fail more than they succeeded . If Object
was a sound technique then they wouldn't have introduced Generics for Collection !
不,这是不可能的。一个数组对象只能有一个数据类型。您可以创建一个同时具有int
和String
作为属性的类并使用它。Object[][]
即使有这样做的诱惑,也不要使用,这是一种邪恶的解决方法,黑客失败的次数比成功的多。如果Object
是一种健全的技术,那么他们就不会引入用于集合的泛型!
回答by Pshemo
You can create Objects 2D array and place there Strings and Integers, but I am not sure if it is good idea to have mixed types in arrays. You should probably describe your problem more so we could figure out better way.
您可以创建对象 2D 数组并将字符串和整数放在那里,但我不确定在数组中混合类型是否是个好主意。您可能应该更多地描述您的问题,以便我们找到更好的方法。
回答by CarlosB
Yes it is. If you declare as a Matrix of object then you can store string and Integer (not int), the difficulty will be after to retrieve them correctly :)
是的。如果您声明为对象的矩阵,那么您可以存储字符串和整数(不是整数),困难将是正确检索它们之后:)
回答by Funkytown
You could do it if you do a 2D array of Object
as in Object[][] myArray = new Object[x][y]
where x and y are numbers.
如果你做一个二维数组,你可以做到Object
,Object[][] myArray = new Object[x][y]
其中 x 和 y 是数字。
All you would have to do is cast the Objects to their expected types before using them. Like (String) myArray[0][3]
for example.
您所要做的就是在使用对象之前将它们转换为它们预期的类型。喜欢(String) myArray[0][3]
的例子。
YOu should only do it this way if you know for certain what type the Object in a particular location will be.
如果您确定特定位置中的对象将是什么类型,您应该只这样做。
However, it's generally not a good idea to do things this way. A better solution would be to define your own data structure class that has a String array and an int array as member variables. As in:
但是,以这种方式做事通常不是一个好主意。更好的解决方案是定义您自己的数据结构类,该类具有一个 String 数组和一个 int 数组作为成员变量。如:
public class myData {
String[] theStringArray;
int[] theIntArray;
public myData(String[] sArraySize, int[] iArraySize) {
this.theStringArray = new String[sArraySize];
this.theIntArray = new int[iArraySize);
}
...
// Additional getters / setters etc...
...
}
回答by JREN
You can create an array of the type Object and store any non-primitive Object in there. When you retrieve them, you'll need to make sure you check their class though.
您可以创建一个 Object 类型的数组并将任何非原始 Object 存储在其中。当您检索它们时,您需要确保检查它们的类。
if(objArray[0] instanceof String) {
// do string stuff
} else if(objArray[0] instanceof Integer) {
// do integer stuff
}
etc.
等等。
I think you're better off creating a new class that can store objects of the types that you want and just retrieve them using getters and setters. It's a lot safer and more stable.
我认为您最好创建一个新类,该类可以存储您想要的类型的对象,并使用 getter 和 setter 检索它们。它更安全,更稳定。