[英]Working with multidimensional dynamic arrays in Java
Suppose that I'm given an object class and an int[]
describing the arbitrary lengths of an multi-dimensional array that I wish to instantiate (and work with). 假设我给了一个对象类和一个int[]
描述我希望实例化(和使用)的多维数组的任意长度。 So far, I've worked out that I can use java.lang.reflect.Array.newInstance(Class<?> type, int... sizes)
, but at that point I'm stuck. 到目前为止,我已经知道我可以使用java.lang.reflect.Array.newInstance(Class<?> type, int... sizes)
,但是那时我被卡住了。 In other words, 换一种说法,
Class<?> type = float.class; // this varies (I receive this)
int[] sizes = new int[]{ 10, 400, 300 }; // this varies too, it could be e.g. int[]{0} for a scalar
Object o = (Object) java.lang.reflect.Array.newInstance(type, sizes);
After defining Object o
I don't know how to proceed. 定义Object o
我不知道如何继续。 A priori, I don't know if type
is going to be float.class
or some other type (for now I assume it's a basic type). 先验,我不知道type
是否将是float.class
或其他类型(现在我认为它是一个基本类型)。 Worse than that, int[] sizes
can be anything. 更糟糕的是, int[] sizes
可以是任何东西。
To make it a more concrete problem, how am I supposed to, say, set each element of the multi-dimensional array o
with a given value, say 5.6
after I've instantiated it with Array.newInstance
? 为了使它成为一个更具体的问题,我应该如何设置多维数组的每个元素o
给定的值,比如我用Array.newInstance
实例化之后的5.6
呢?
Good question. 好问题。 This is really tricky because there doesn't seem to be a way to cast an object to an array of given dimensions. 这真的很棘手,因为似乎没有办法将对象强制转换为给定维度的数组。 I couldn't find a way to get a proper multidimensional array object out of the created instance. 我找不到从创建的实例中获取正确的多维数组对象的方法。 But if you just want to get and set elements in the created array, you can use the following method: 但是,如果您只想在创建的数组中获取和设置元素,则可以使用以下方法:
public static void main(String[] args) {
Class<?> type = float.class; // this varies (I receive this)
int[] sizes = new int[] { 1, 3 }; // this varies too, it could be e.g.
// int[]{0} for a scalar
Object f = Array.newInstance(type, sizes);
set(f, new int[] { 0, 2 }, 3.0f);
if (f instanceof Object[])
System.out.println(Arrays.deepToString((Object[]) f));
else {
int l = Array.getLength(f);
for (int i = 0; i < l; ++i) {
System.out.print(Array.get(f, i) + ", ");
}
System.out.println();
}
}
public static void set(Object arr, int[] indexPath, Object value) {
if (arr instanceof Object[]) {
Object[] temp= (Object[]) arr;
for (int i = 0; i < indexPath.length - 2; ++i) {
temp = (Object[]) temp[indexPath[i]];
}
Array.set(temp[indexPath[indexPath.length - 2]],
indexPath[indexPath.length - 1], value);
} else {
Array.set(arr,
indexPath[0], value);
}
}
The set
method takes the index to set as an array. set
方法将索引设置为数组。 So set(f, new int[] {0,0,1}, 3.0f);
所以set(f, new int[] {0,0,1}, 3.0f);
should basically do f[0][0][1] = 3.0f
应该基本上做f[0][0][1] = 3.0f
EDIT : Added a slightly ugly fix to handle single dimensional arrays 编辑 :添加了一个稍微丑陋的修复来处理单维数组
Looking in the java.lang.reflect.Array
class, there are other useful methods, such as get(Object array, int index)
and setFloat(Object array, int index, float f)
. 查看java.lang.reflect.Array
类,还有其他有用的方法,例如get(Object array, int index)
和setFloat(Object array, int index, float f)
。 Using combinations of these, one should be able to fill the entire multi-dimensional array. 使用这些的组合,应该能够填充整个多维阵列。
Rough pseudocode: 粗伪代码:
Object level0 = createArray(...)
For i1 = 0 to (length of dimension 1) {
Object level1 = get(level0, i1)
For i2 = 0 to (length of dimension 2) {
Object level2 = get(level1, i2)
...etc...
For iM = 0 to (length of dimension M) {
Object levelM = get(levelL, iM)
For iN = 0 to (length of dimension N) {
setFloat(levelM, iN, 5.6f)
}
}
...
}
}
If I'm reading the API correctly, something like this should work. 如果我正确地阅读API,这样的事情应该有效。
I ended up coding the solution instead of attempting to find/use array manipulation methods from the Java API (the only one below is java.util.Arrays.copyOf(Object[], int)
. 我最终编写了解决方案,而不是尝试从Java API中查找/使用数组操作方法(下面唯一的一个是java.util.Arrays.copyOf(Object[], int)
。
So, below are the methods I wrote. 所以,下面是我写的方法。 I did not test them extensively (I wouldn't be surprised if there are inefficiencies there, I just tried the first thing that appeared to work), so I expect that the following solution can be adapted to suit others' needs: 我没有广泛地测试它们(如果那里效率低下我不会感到惊讶,我只是尝试了第一件似乎有用的东西),所以我希望以下解决方案可以适应其他人的需求:
/**
* Creates an instance of Object multi-dimensional arrays, with dimensions specified by the argument.
*
* @example Create an array Object[4][20][30]:
* <pre>
* Object[] array = newArray(new int[]{4,20,30});
* </pre>
*
* @param sizes The list of dimension lengths.
* @return
*/
public static Object[] newArray(int[] sizes) {
Object[] result = null;
if (sizes != null) {
for(int n = sizes.length - 1; n >= 0; n--) {
if (result == null) {
result = new Object[sizes[n]];
} else {
Object[] oa = new Object[sizes[n]];
for (int i = 0; i < sizes[n]; i++) {
oa[i] = java.util.Arrays.copyOf(result, result.length);
}
result = oa;
}
}
if (result == null) { result = new Object[1]; }
}
return result;
}
/**
* Get the value of a multi-dimensional array element given by a coordinate list.
*
* @example Read the value at [2][14][27]:
* <pre>
* Object[] array; // e.g. a int[4][20][30] created with newArray(int[])
* int[] coord = new int[]{2,14,27};
* Object value = getValue(array, coord);
* </pre>
*
* @param array The coordinates of the array element.
* @param coordinates
* @return
*/
public static Object getValue(Object[] array, int[] coordinates) {
Object result = null;
if (array == null || coordinates == null || 0 > coordinates[0]||coordinates[0] > array.length) {
result = null;
} else {
int x = coordinates[0];
if (array[x] instanceof Object[]) {
int[] c = new int[coordinates.length-1];
for(int i = 0; i < c.length; i++) { c[i] = coordinates[i + 1]; }
result = getValue((Object[]) array[x], c);
} else {
result = array[x];
}
}
return result;
}
/**
* Set the value of a multi-dimensional array element given a list designating the element's coordinates.
*
* @example Write a value to [1][0][7]:
* <pre>
* Object value; // e.g. a float
* Object[] array; // e.g. a int[4][20][30] created with newArray(int[])
* int[] coord = new int[]{1,0,7,};
* setValue(array, coord, value);
* </pre>
*
* @param array
* @param coordinates
* @param value
* @return
*/
public static void setValue(Object[] array, int[] coordinates, Object value) {
if (array == null || coordinates == null || array.length == 0 || coordinates.length == 0 || array.length < coordinates[0]||coordinates[0] < 0 ) {
return;
} else {
int x = coordinates[0];
if (array[x] != null && array[x].getClass().isArray()) { // recurse
int[] c = new int[coordinates.length - 1];
for (int i = 0; i < c.length; i++) { c[i] = coordinates[i + 1]; }
setValue((Object[]) array[x], c, value);
} else {
array[x] = value;
}
}
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.