1

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.

3
  • You need to add more layers of array list (although for some use cases using layers of maps is better) Commented Nov 9, 2018 at 7:40
  • You may use a map inside one or more layers of ArrayList. For example: ArrayList<HashMap<Integer,Integer>> Commented Nov 9, 2018 at 7:42
  • 3
    An alternative is to create a key class made of 3 numbers (x, y, z) and making sure equals and hash code are implemented correctly. Then you can use a map from your key class to a value. Commented Nov 9, 2018 at 7:43

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I should note that it’s better to use the @Value annotation as it creates a (more) mutable version of Key.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.