Melanie A
Melanie A

Reputation: 269

Creating a dynamic 3d array in Java

I have a piece of information that depends on 3 different factors (which I represent as numbers), for example,

if 0,0,0 then information is (value) if 0,0,1, then information is (value) etc.

I initially created a 3d array, and filled it with nested for loops, something like

   for (int i = 0; i < maxfactor1; i++) {
        for (int a = 0; a < maxfactor2; a++) {
            for (int j = 0; j < maxfactor3; j++) {
                test2[i][a][j] = j * 2.0;
                System.out.println(test2[i][a][j]);
            }
        }
    }

The problem is that I may not have every single combination of factors, and including them would be wrong. I searched for a dynamic implementation of the array, and found something like:

 ArrayList<Integer> row = new ArrayList<Integer>();
    row.add(1);
    row.add(11);
    row.add(111);
    row.add(2);
    row.add(22);
    row.add(222);

 ArrayList<ArrayList<Integer>> test = new ArrayList<ArrayList<Integer>>();
 test.add(row);

The problem is that although I am able to store the factors, I am not able to store the value. I need to be able to easily access the factors (like in a for loop) as well as the value.

Are there any other ways to create a dynamic size array in Java so that I can refer to both factors and the value? Any advice is mostly appreciated.

Upvotes: 1

Views: 1151

Answers (2)

eigenharsha
eigenharsha

Reputation: 2231

You can try with HashMap<String, Integer>. The indexes like [1],[1],[0], So we can make it as key = "1,1,0", and assign the value for that key.

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    private class Array3D {
        Map<String, Integer> arr = new HashMap<String, Integer>();

        public void add(int i, int j, int k, int value) {
            arr.put(i + "," + j + "," + k, value);
        }

        public int get(int i, int j, int k) {
            return arr.get(i + "," + j + "," + k);
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Array3D arr = new Ideone().new Array3D();

        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                for(int k = 0; k < 3; k++) {

                    arr.add(i, j, k, i+j+k + 55);
                    System.out.println(arr.get(i,j,k));
                }
            }
        }
    }
}

run :: https://ideone.com/40WugA

Upvotes: 1

robjwilkins
robjwilkins

Reputation: 5652

An example of the solution suggested by @Roy Shahaf. Create an object for use as the key in a Map

//Lombok annotations to generate Getters,Setters,Constructor,Equals and Hashcode methods
@Data @AllArgsContructor @EqualsAndHashCode
public class Key {
    private Integer x;
    private Integer y;
    private Integer z;
}

You can then use this as the key for data stored in a Map:

@Test
public void storeDataInMapWithCompositeKey() {
    Map<Key, Integer> myMap = new HashMap<>();
    myMap.put(new Key(0,0,0), 0);
    myMap.put(new Key(0,0,1), 1);
}

Upvotes: 3

Related Questions