I want a function / data structure that can do this:
我想要一个能够做到这一点的函数/数据结构:
func(int dim){
if(dim == 1)
int[] array;
else if (dim == 2)
int[][] array;
else if (dim == 3)
int[][][] array;
..
..
.
}
anyone know how?
有人知道吗?
4 个解决方案
#1
4
Edit
编辑
Or you could use Array.newInstance(int.class, sizes). Where sizes is an int[]
containing the desired sizes. It will work better because you could actually cast the result to an int[][][]...
或者可以使用Array.newInstance(int)。类,大小)。其中大小是一个int[],包含所需的大小。它会更好地工作,因为您可以将结果转换为int[][][][][]…
Original Answer
原来的答案
You could use the fact that both int[]
and Object[]
are Object
s. Given that you want a rectangular multidimensional array with sizes given by the list sizes
您可以使用int[]和Object[]都是对象这一事实。假设您想要一个矩形的多维数组,其大小由列表大小决定
Object createIntArray(List<Integer> sizes) {
if(sizes.size() == 1) {
return new int[sizes.get(0)];
} else {
Object[] objArray = new Object[sizes.get(0)];
for(int i = 0; i < objArray.length; i++) {
objArray[i] = createIntArray(sizes.subList(1, sizes.size());
}
return objArray;
}
}
You lose all static type checking, but that will happen whenever you want a dynamically dimensioned array.
您将丢失所有静态类型检查,但无论何时需要动态维度数组,都将发生这种情况。
#2
1
If your purpose is to create a truly dynamic array, then you should look at the Array object in the JDK. You can use that to dynamically generate an array of any dimension. Here is an example:
如果您的目的是创建一个真正的动态数组,那么您应该查看JDK中的数组对象。您可以使用它动态地生成任意维度的数组。这是一个例子:
public void func(int dim) {
Object array = Array.newInstance(int.class, new int[dim]);
// do something with the array
}
Once the array Object has been created, you can use the methods of the java.lang.reflect.Array class to access, add, remove elements from the multi-dimension array that was created. In also includes utility methods to determine the length of the array instance.
创建数组对象之后,可以使用java.lang. reflection的方法。数组类来访问、添加、从创建的多维数组中删除元素。还包括确定数组实例长度的实用方法。
You can even check the dimension of the array using:
你甚至可以使用:
public int getDimension(Object array) {
int dimension = 0;
Class cls = array.getClass();
while (cls.isArray()) {
dimension++;
cls = cls.getComponentType();
}
return dimension;
}
#3
1
People have post good solutions already, but I thought it'd be cool (and good practice) if you wrap the dynamic multidimensional array into a class, which can use any data structure to represent the multi-dimensional array. I use hash table so you have virtually unlimited size dimensions.
人们已经发布了很好的解决方案,但我认为,如果将动态多维数组封装到一个类中(该类可以使用任何数据结构来表示多维数组),这将是一个很酷的(也是很好的实践)。我使用哈希表,所以你有无限的尺寸。
public class MultiDimArray{
private int myDim;
private HashMap myArray;
public MultiDimArray(int dim){
//do param error checking
myDim = dim;
myArray= new HashMap();
}
public Object get(Integer... indexes){
if (indexes.length != myDim){throw new InvalidArgumentException();}
Object obj = myArray;
for (int i = 0; i < myDim; i++){
if(obj == null)
return null;
HashMap asMap = (HashMap)obj;
obj = asMap.get(indexes[i]);
}
return obj;
}
public void set(Object value, Integer... indexes){
if (indexes.length != myDim){throw new InvalidArgumentException();}
HashMap cur = myArray;
for (int i = 0; i < myDim - 1; i++){
HashMap temp = (HashMap)cur.get(indexes[i]);
if (temp == null){
HashMap newDim = new HashMap();
cur.put(indexes[i], newDim);
cur = newDim;
}else{
cur = temp;
}
}
cur.put(indexes[myDim -1], value);
}
}
and you can use the class like this:
你可以用这个类
Object myObj = new Object();
MultiDimArray array = new MultiDimArray(3);
array.put(myObj, 0, 1, 2);
array.get(0, 1, 2); //returns myObj
array.get(4, 5, 6); //returns null
#4
0
What about a class like following?
像follow这样的课程怎么样?
class DynaArray {
private List<List> repository = new ArrayList<List>();
public DynaArray (int dim) {
for (int i = 0; i < dim; i++) {
repository.add(new ArrayList());
}
}
public List get(int i) {
return repository.get(i);
}
public void resize(int i) {
// resizing array code
}
}
#1
4
Edit
编辑
Or you could use Array.newInstance(int.class, sizes). Where sizes is an int[]
containing the desired sizes. It will work better because you could actually cast the result to an int[][][]...
或者可以使用Array.newInstance(int)。类,大小)。其中大小是一个int[],包含所需的大小。它会更好地工作,因为您可以将结果转换为int[][][][][]…
Original Answer
原来的答案
You could use the fact that both int[]
and Object[]
are Object
s. Given that you want a rectangular multidimensional array with sizes given by the list sizes
您可以使用int[]和Object[]都是对象这一事实。假设您想要一个矩形的多维数组,其大小由列表大小决定
Object createIntArray(List<Integer> sizes) {
if(sizes.size() == 1) {
return new int[sizes.get(0)];
} else {
Object[] objArray = new Object[sizes.get(0)];
for(int i = 0; i < objArray.length; i++) {
objArray[i] = createIntArray(sizes.subList(1, sizes.size());
}
return objArray;
}
}
You lose all static type checking, but that will happen whenever you want a dynamically dimensioned array.
您将丢失所有静态类型检查,但无论何时需要动态维度数组,都将发生这种情况。
#2
1
If your purpose is to create a truly dynamic array, then you should look at the Array object in the JDK. You can use that to dynamically generate an array of any dimension. Here is an example:
如果您的目的是创建一个真正的动态数组,那么您应该查看JDK中的数组对象。您可以使用它动态地生成任意维度的数组。这是一个例子:
public void func(int dim) {
Object array = Array.newInstance(int.class, new int[dim]);
// do something with the array
}
Once the array Object has been created, you can use the methods of the java.lang.reflect.Array class to access, add, remove elements from the multi-dimension array that was created. In also includes utility methods to determine the length of the array instance.
创建数组对象之后,可以使用java.lang. reflection的方法。数组类来访问、添加、从创建的多维数组中删除元素。还包括确定数组实例长度的实用方法。
You can even check the dimension of the array using:
你甚至可以使用:
public int getDimension(Object array) {
int dimension = 0;
Class cls = array.getClass();
while (cls.isArray()) {
dimension++;
cls = cls.getComponentType();
}
return dimension;
}
#3
1
People have post good solutions already, but I thought it'd be cool (and good practice) if you wrap the dynamic multidimensional array into a class, which can use any data structure to represent the multi-dimensional array. I use hash table so you have virtually unlimited size dimensions.
人们已经发布了很好的解决方案,但我认为,如果将动态多维数组封装到一个类中(该类可以使用任何数据结构来表示多维数组),这将是一个很酷的(也是很好的实践)。我使用哈希表,所以你有无限的尺寸。
public class MultiDimArray{
private int myDim;
private HashMap myArray;
public MultiDimArray(int dim){
//do param error checking
myDim = dim;
myArray= new HashMap();
}
public Object get(Integer... indexes){
if (indexes.length != myDim){throw new InvalidArgumentException();}
Object obj = myArray;
for (int i = 0; i < myDim; i++){
if(obj == null)
return null;
HashMap asMap = (HashMap)obj;
obj = asMap.get(indexes[i]);
}
return obj;
}
public void set(Object value, Integer... indexes){
if (indexes.length != myDim){throw new InvalidArgumentException();}
HashMap cur = myArray;
for (int i = 0; i < myDim - 1; i++){
HashMap temp = (HashMap)cur.get(indexes[i]);
if (temp == null){
HashMap newDim = new HashMap();
cur.put(indexes[i], newDim);
cur = newDim;
}else{
cur = temp;
}
}
cur.put(indexes[myDim -1], value);
}
}
and you can use the class like this:
你可以用这个类
Object myObj = new Object();
MultiDimArray array = new MultiDimArray(3);
array.put(myObj, 0, 1, 2);
array.get(0, 1, 2); //returns myObj
array.get(4, 5, 6); //returns null
#4
0
What about a class like following?
像follow这样的课程怎么样?
class DynaArray {
private List<List> repository = new ArrayList<List>();
public DynaArray (int dim) {
for (int i = 0; i < dim; i++) {
repository.add(new ArrayList());
}
}
public List get(int i) {
return repository.get(i);
}
public void resize(int i) {
// resizing array code
}
}