Java的映射多维数组单挑 [英] Java- Mapping multi-dimensional arrays to single

查看:127
本文介绍了Java的映射多维数组单挑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相对于另一悬而未决的问题,我有张贴这,但是我认为这应该得到它自己的问题。

备用的问题(仅供参考): Java代理情迷博特

基本上,我需要存储数据量非常大,并具有可访问速度非常快。这将在无限的存储情况理想工作:

 布尔[] [] [] [] =套新的布尔[256] [256] [256] [256];
布尔的get(字节[] A){
    返回集[一个[0] [一个[1]] [一个[2] [A [3]];
}

不过,这大约使用的RAM和16GB这实在是太多了我的应用程序。我认为,如果使用位而不是布尔(如存储在Java中的4个字节),这将削减内存使用512MB左右。不过,我似乎无法换我围绕如何正确地访问位头。例如,如果你映射的每个地址是这样的:位置= A * B * C * D那么它会映射到相同的位为d * C * B *一个等

我发现这个主题涵盖了如何二维数组转换成一维数组,但我似乎无法换我围绕着如何延伸到一个四维阵列头。任何人都可以解释一下吗?
映射一个二维数组到一个一维列C

有关2D解决方案 - >一维数组:

  int数组[宽*高]。
INT SetElement(INT行,诠释山口,int值)
{
  数组[宽*行+ COL] =价值;
}

我只是不知道如何把它扩大到4D - > 1D

  int数组[256 * 256 * 256 * 256];
 INT setElement(int类型的,INT B,INT C,INT D,布尔值)
 {
    数组[?????????] =价值;
 }


解决方案

要回答有关映射4D至1D,如果可视化,比如说,一个棋盘,你可以拿出公式2D通过,如果一切有思想的1D行有宽度元素,而我先下去的行数,然后移动到 COL ,然后我在宽*行+ COL 。现在想象的高度棋盘数堆栈,并进行同样的运动把它扩大到三个维度。四,外形尺寸是困难,因为你真的不能想象它,但那时你可以看到的模式。

该计划显示了四个维度的公式。我跑它非常小的数字张贴在这里,但你可以用维玩,看看它是如何工作的。

 类点心
{
    //尺寸
    静态INT D1 = 2; //行
    静态INT D2 = 2; //COLS
    静态INT D3 = 3; //高度
    静态INT D4 = 2; //第四维!    公共静态无效的主要(字串[] args){
        的for(int i = 0; I< D​​1,我++){
            对于(INT J = 0; J< D​​2; J ++){
                对于(INT K = 0; K< D​​3; k ++){
                    为(中间体m为0; M&下; D4; M +){
                        INT oneD = fourDtoOneD(I,J,K,M);
                        System.out.printf((%D,%D,%D,%D) - >%d个\\ N,I,J,K,M,oneD);
                    }
                }
            }
        }
    }    静态INT fourDtoOneD(INT I,诠释J,诠释K,INT M){
        返回((D2 * D3 * D4)* I)+((D2 * D3)* j)条+(D2 * k)的+ M;
    }
}
$ java的点心
(0,0,0,0) - > 0
(0,0,0,1) - > 1
(0,0,1,0) - > 2
(0,0,1,1) - > 3
(0,0,2,0) - > 4
(0,0,2,1) - >五
(0,1,0,0) - > 6
(0,1,0,1) - > 7
(0,1,1,0) - > 8
(0,1,1,1) - > 9
(0,1,2,0) - > 10
(0,1,2,1) - > 11
(1,0,0,0) - > 12
(1,0,0,1) - > 13
(1,0,1,0) - > 14
(1,0,1,1) - > 15
(1,0,2,0) - > 16
(1,0,2,1) - > 17
(1,1,0,0) - > 18
(1,1,0,1) - > 19
(1,1,1,0) - > 20
(1,1,1,1) - > 21
(1,1,2,0) - > 22
(1,1,2,1) - > 23

I am posting this in relation to another open question i have, however I thought that this deserved it's own question.

Alternate question (for reference): Java Proxy Discovering Bot

Basically, I need to store a very large amount of data and have it accessible very quickly. This would work ideally in an unlimited memory situation:

boolean[][][][] sets = new boolean[256][256][256][256];
boolean get(byte[] a) {
    return sets[a[0]][a[1]][a[2]][a[3]];
}

However, this uses around 16gb of ram which is too much for my application. I figure that if using bits instead of booleans (stores as 4 bytes in Java) it would cut the memory usage to around 512MB. However, I can't seem to wrap my head around how to access the bits correctly. For example if you mapped each address something like this: position = a * b * c * d then it would map to the same bit as d * c * b * a etc.

I found this thread covering how to convert 2D arrays into 1D arrays, but I can't seem to wrap my head around how to extend that to a 4D array. Can anyone explain this? Map a 2D array onto a 1D array C

The solution for 2D -> 1D arrays:

int array[width * height];
int SetElement(int row, int col, int value)
{
  array[width * row + col] = value;  
}

I am just not sure of how to extend it to 4D -> 1D

 int array[256 * 256 * 256 * 256];
 int setElement(int a, int b, int c, int d, boolean value)
 {
    array[?????????] = value;  
 }

解决方案

To answer about mapping 4D to 1D, if you visualize, say, a chess board, you can come up with the formula for 2D to 1D by thinking if every row has width elements, and I first go down row number of rows and then move over to col, then I'm at width * row + col. Now imagine a stack of height number of chess boards and carry out the same exercise to extend it to three dimensions. Four dimensions is harder because you can't really visualize it, but by then you can see the pattern.

This program shows the formula for four dimensions. I ran it for very small numbers for posting here, but you can play with the dimensions and see how it works.

class Dim
{
    // dimensions
    static int d1 = 2 ;   // "rows"
    static int d2 = 2;    // "cols"
    static int d3 = 3;    // "height"
    static int d4 = 2;    // the fourth dimension!

    public static void main(String[] args) {
        for (int i=0; i<d1; i++) {
            for (int j=0; j<d2; j++) {
                for (int k=0; k<d3; k++) {
                    for (int m=0; m<d4; m++) {
                        int oneD = fourDtoOneD(i, j, k, m);
                        System.out.printf("(%d, %d, %d, %d) -> %d\n", i, j, k, m, oneD);
                    }
                }
            }
        }
    }

    static int fourDtoOneD(int i, int j, int k, int m) {
        return ((d2*d3*d4) * i) + ((d2*d3) * j) + (d2 * k) + m;
    }
}


$ java Dim
(0, 0, 0, 0) -> 0
(0, 0, 0, 1) -> 1
(0, 0, 1, 0) -> 2
(0, 0, 1, 1) -> 3
(0, 0, 2, 0) -> 4
(0, 0, 2, 1) -> 5
(0, 1, 0, 0) -> 6
(0, 1, 0, 1) -> 7
(0, 1, 1, 0) -> 8
(0, 1, 1, 1) -> 9
(0, 1, 2, 0) -> 10
(0, 1, 2, 1) -> 11
(1, 0, 0, 0) -> 12
(1, 0, 0, 1) -> 13
(1, 0, 1, 0) -> 14
(1, 0, 1, 1) -> 15
(1, 0, 2, 0) -> 16
(1, 0, 2, 1) -> 17
(1, 1, 0, 0) -> 18
(1, 1, 0, 1) -> 19
(1, 1, 1, 0) -> 20
(1, 1, 1, 1) -> 21
(1, 1, 2, 0) -> 22
(1, 1, 2, 1) -> 23

这篇关于Java的映射多维数组单挑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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