Best way to create 5D or 6D array in java -
i working on redesigning of code in 6d array created . required cplex linear calculation.
now, there api's can support 1d or int variable in cplex not important have 6 dimension array. mapping important variable required when retrieing method.
is there way of replacing 6d array. in simplying complexity of code.
-> can replace somehow 6d array , store variable in 1d array or int variable.. -> tried hashmap key list , value integer taking more time because hashmap internally uses array.
sample code is:
int [][][][][][] testvariable = new int [2][2][3][5][2][2]; hashmap<list<integer>, integer> mapping = new hashmap<list<integer>, integer>(); int[] arrayadd = new int[11111]; (int =0; <= 2; a++) { (int l = 0; l < 2; l++) { (int y = 0; y < 3; y++) { (int r = 0; r < 5; r++) { for(int oc=0; oc< 2; oc++){ for(int c=0; c< 2; c++){ // usual array of 6d testvariable[a][l][y][r][oc][c]=count; // first method ( takes more time array) linkedlist<integer> listadd = new linkedlist<integer>(); listadd.add(a); listadd.add(l); listadd.add(y); listadd.add(r); listadd.add(oc); listadd.add(c); mapping.put(listadd, count); // second method .. still testing doesnt make sense) arrayadd[a + l + y + r + oc + c] = count; // arrays.fill(arrayadd, count); //system.out.println("value of list " + listadd); count ++; } } } } } }
if know each of dimensions' lengths won't change, can simulate 6d array 1d array. trick multiply indexes last index c high enough number there won't collisions between distinct sets of 6d indexes mapping same 1d index.
int[] onedarray = new int[2*2*3*5*2*2]; and calculation of 1d index, using 6 6d indexes. multiplying product of dimensions' lengths after each particular 6d index avoid collisions.
int onedindex = 2*3*5*2*2 * // last 5 dimensions' lengths + 3*5*2*2 * l // last 4 dimensions' lengths; variable letter l + 5*2*2 * y // last 3 dimensions' lengths + 2*2 * r // last 2 dimensions' lengths + 2 * oc // last dimension's length + c; // no multiplier necessary last index it better use constants each of dimensions' lengths here, reduce hard-coding.
Comments
Post a Comment