在运行时数组的秩 [英] Set rank of array at runtime

查看:105
本文介绍了在运行时数组的秩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么最简单的方法是执行数组谁的排名在运行时指定。

I was wondering what the simplest way would be to implement an array who's rank is specified at runtime.

我在商店工作了晶格点布尔值的数组,这个例子我希望用户能够选择的模型有多少空间维度在运行时使用。

The example I am working on stores a array of boolean values for lattice points, and I want the user to be able to chose how many spatial dimensions the model uses at runtime.

我看了Array.newInstance()方法:

I've looked at the Array.newInstance() method:

dimensionOfSpace = userInputValue;  // this value comes from GUI or whatever
int latticeLength = 5;  // square lattice for simplicity

int[] dimensions = new int[dimensionOfSpace];
for(int i = 0; i < l.length; i++) l[i] = length; 
Object lattice = Array.newInstance(boolean.class, dimensions);

但在任何形式的方式来访问这些值似乎需要异常缓慢的方法,如使用递归直到Array.get返回值不再是一个数组,即使用IsArray的()。

But accessing these values in any sort of way seems to require horribly slow methods such as recursively using Array.get until the returned value is no longer an array, i.e. using isArray().

我缺少一个明显的解决方案吗?我很想能够访问类似foo的方式值[I] [J] [K]

Am I missing an obvious solution here? I would love to be able to access the values in a way similar to foo[i][j][k].

推荐答案

看起来是你正在寻找一些方法来声明一个数组多少维度具有在运行时。我不知道如何可以用多维的ArrayList,或任何多维结构,你必须在编译时指定的维度来完成。

Looks like what you are looking for is for some way to declare how many dimensions an array has at runtime. I don't know how this could be done using a multidimensional ArrayList, or any multidimensional structure where you have to specify the dimensionality at compile time.

我看到的唯一的答案是使用包裹在该转换多维坐标和从底层阵列中的其位置的一类简单的线性阵列。这基本上是如何语言如C存储多维数组通过使用存储器的一个连续的块

The only answer I see is to use a simple linear array wrapped in a class that converts multidimensional coordinate to and from the its position in the underlying array. This is basically how languages such as C stores multidimensional arrays by using one contiguous chunk of memory.

在code会是这个样子:

The code would look something like this:

import java.util.*;

class MultiArray<T>{
    private int[] dimensions;
    private Object[] array;

    public MultiArray(int ... dimensions){
        this.dimensions=dimensions;
        //Utils.product returns the product of the ints in an array
        array=new Object[Utils.product(dimensions)];
    }

    public void set(T value, int ... coords){
        int pos=computePos(coords); 
        array[pos]=value;
    }

    public T get(int ... coords){
        int pos=computePos(coords);
        return (T)(array[pos]);
    }

    private int computePos(int[] coords){
        int pos=0;
        int factor=1;
        for (int i=0;i<coords.length;i++){
            pos+=factor*coords[i];
            factor*=dimensions[i];
        }
        return pos;
    }
}

class Main{
    public static void main(String args[]){
        MultiArray<Integer> m=new MultiArray<Integer>(new int[]{5,4,3}); 
        Random r=new Random();

        for(int i=0;i<5;i++)
            for(int j=0;j<4;j++)
                for(int k=0;k<3;k++)
                    m.set(r.nextInt(),i,j,k);
        for(int i=0;i<5;i++){
            for(int j=0;j<4;j++){
                for(int k=0;k<3;k++)
                    System.out.print(m.get(i,j,k)+" ");     
                System.out.println("");
            }
            System.out.println("\n");
        }
    }
}

class Utils{
    public static int product(int...a){
        int ret=1;
        for (int x:a) ret*=x;
        return ret;
    } 
}

这篇关于在运行时数组的秩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆