假设我们有多维数组,并且仅在运行时知道维数。假设我们有一个整数个索引。
如何将索引应用于数组以便访问数组的元素?
更新
假设:
int [] indices = new int { 2, 7, 3, ... , 4}; // indices of some element
int X = indices.length; // number of dimensions
Object array = .... // multidimensional array with number of dimensions X
...
我想从indices
获取由array
索引处理的元素。
更新2
我根据递归编写了以下代码:
package tests;
import java.util.Arrays;
public class Try_Multidimensional {
private static int element;
public static int[] tail(int[] indices) {
return Arrays.copyOfRange(indices, 1, indices.length);
}
public static Object[] createArray(int ... sizes) {
Object[] ans = new Object[sizes[0]];
if( sizes.length == 1 ) {
for(int i=0; i<ans.length; ++i ) {
ans[i] = element++;
}
}
else {
for(int i=0; i<ans.length; ++i) {
ans[i] = createArray(tail(sizes));
}
}
return ans;
}
public static Object accessElement(Object object, int ... indices) {
if( object instanceof Object[] ) {
Object[] array = (Object[]) object;
return accessElement(array[indices[0]], tail(indices));
}
else {
return object;
}
}
public static void main(String[] args) {
element = 0;
Object array = createArray(4, 5, 12, 7);
System.out.println(accessElement(array, 0, 0, 0, 0));
System.out.println(accessElement(array, 0, 0, 0, 1));
System.out.println(accessElement(array, 1, 0, 10, 0));
try {
System.out.println(accessElement(array, 0, 5, 0, 1));
}
catch(Exception e) {
System.out.println(e.toString());
}
System.out.println(4*5*12*7-1);
System.out.println(accessElement(array, 3, 4, 11, 6));
}
}
问题是:
1)JDK和/或着名的图书馆是否有任何可靠的现成方法?
2)我正在使用Object
。可以避免吗?我可以创建/访问内置或特定类型的变量维度数组吗?使用Object
会产生多大的回报?
答案 0 :(得分:2)
int index(Object arrayToIndex, int... indices) {
for (int i = 0; i < indices.length - 1; i++) {
arrayToIndex = ((Object[]) arrayToIndex)[indices[i]];
}
return ((int[]) arrayToIndex)[indices[indices.length-1]];
}
循环维度并为每个维度编制索引,一次一个。演员表和最后一个维度的特殊情况会很烦人,所以我建议将它包装在某种n维数组类中。 (It looks like some options already exist.)
答案 1 :(得分:0)
您可以将每个维度的大小发现为单个数组(因为它们就是这样):
public void someMEthod(int[][][] matrix) {
int d1 = matrix.length;
int d2 = 0;
int d3 = 0;
if(d1 > 0) {
d2 = matrix[0].length;
if(d2 > 0) {
d3 = matrix[0][0].length;
}
}
System.out.println("Dimension 1 is " + d1);
System.out.println("Dimension 2 is " + d2);
System.out.println("Dimension 3 is " + d3);
}
我希望有所帮助。
答案 2 :(得分:0)
我找到了一种有趣的方式来使用反射来做到这一点。这只是我扔在一起的一些代码,但是你可以将它包装在一个类中并使它完全漂亮。
// build and fill an array to the given depth
public static Object[] constructArray(Object[] array, int depth) {
if(depth == 0)
return null;
for(int i=0;i<array.length;i++) {
Array.set(array, i, constructArray(new Object[array.length], depth-1));
}
return array;
}
// sets a value in the multi dimensional array using the indicies
public static void setArrayUsingIndecies(Object array, int[] indicies, Object value) {
if(indicies.length == 0)
return;
for(int i=0;i<indicies.length-1;i++) {
array = Array.get(array, indicies[i]);
}
Array.set(array, indicies[indicies.length-1], value);
}
// gets a value in the multi dimmensional array using the indicies
public static Object getArrayUsingIndecies(Object array, int[] indicies) {
Object value = array;
for(int i=0;i<indicies.length;i++) {
value = Array.get(value, indicies[i]);
}
return value;
}
下面是一些示例代码
int numberOfDimmensions = 2;
Object array = constructArray(new Object[numberOfDimmensions], numberOfDimmensions);
int [] indices = new int [] { 0, 1 };
setArrayUsingIndecies(array, indices, "Hello");
System.out.println(getArrayUsingIndecies(array, indices)); // Hello
indices = new int [] { 0, 0 };
System.out.println(getArrayUsingIndecies(array, indices)); // null
答案 3 :(得分:0)
它比我们想象的更简单吗?这种方法怎么样:
int [] indices = new int { 2, 7, 3, ... , 4}; // indices of some element
int X = indices.length; // number of dimensions
Object array = new Object[X].... // multidimensional array with number of dimensions X
然后:
Object myObject = array[indices[1]] // myObject references the 7th element of array
你必须确保你的索引数组不包含大于索引大小的数字 - 例如
indices = new int [5,4,3,2,1] // ok
indices = new int [6,4,3,2,1] // not ok, because you would access the 6th Element in an arry with length 5