如何在Java中以编程方式访问多维数组?
假设我们有多维数组,并且只有在运行时才知道维数。假设我们有整数个索引 如何对数组应用索引以访问数组的元素 更新 假设:如何在Java中以编程方式访问多维数组?,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,假设我们有多维数组,并且只有在运行时才知道维数。假设我们有整数个索引 如何对数组应用索引以访问数组的元素 更新 假设: 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 dimensio
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
...
我想从数组
中提取索引索引
寻址的元素
更新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));
}
}
包测试;
导入java.util.array;
公共类Try\u{
私有静态int元素;
公共静态int[]尾(int[]索引){
返回数组.copyOfRange(索引,1,索引.长度);
}
公共静态对象[]createArray(整数…大小){
Object[]ans=新对象[size[0]];
如果(size.length==1){
对于(int i=0;i,您可以发现每个维度作为单个数组的大小(因为这就是它们的大小):
我希望这能有所帮助。int-index(对象数组索引,int…索引){
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]];
}
对于(int i=0;i
循环遍历维度并索引每个维度,一次一个。最后一个维度的强制转换和特例会很烦人,因此我建议将其包装到某种n维数组类中。()我发现了一种使用反射的有趣方法。这只是我编写的一些代码,但您可以将其封装在一个类中,使其更加美观
// 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 [] 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
但是,您必须确保您的索引数组不包含大于索引大小-1的数字
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
你能说得更具体一点吗,也许提供一个代码snipplet?你能提供任何例子吗?我不明白,我建议你阅读。java中没有多维数组..它是数组的数组..@Anirudha我建议你也阅读我提供的链接…维度的数量是可变的,不仅仅是每个维度的大小。我猜p这里的问题是数组的维数未知,因此不能将矩阵
任意声明为int[]
。原始问题已被编辑,因此我不确定这是否真的回答了该问题。当前编写的问题的答案是,您不能这样做。声明数组时,必须知道维度的数量。每个维度的值可以是运行时动态的,但不能是dime的数量使用ArrayList的ArrayList of ArrayList of ArrayList可以做到这一点,在这种情况下,维度的数量可以是动态的,但是关于数组(而不是ArrayList)的问题我想她想创建一个N维数组,其中N维在运行时才知道,而语言不支持它。@JeffScottBrown:这个问题只是关于访问数组。创建它是另一回事。我和你一样误解了这个问题。这个问题包括“对象数组=…..//多维数组,维数为X“这是右手边无法完成的事情。你不能创建具有动态维数的数组,这是我认为她所追求的。也许我对她追求的是错误的。问题已经改变,似乎在问一些不同的问题。我要投降了。”祝OP好运。为什么你不能重写for(inti=0;i
->for(inti=0;i
,最后只返回arrayToIndex;
?这不是来自C#
?对不起,我不明白你的评论?
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