I am trying to map a zero indexed value to a multidimensional array list using the map function so that I can assign data to specific values within the arraylist without having to constantly request that I Dex location. The code I have written is not compiling for some reason.
Could so eone please check it their side please and also criticise where required?
`
package main.java.javatestfiles;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* #####CORRECTED#####
*
* The purpose of the class is to map a cubic calculation to
* a multidimensional arraylist as the partials iterate over
* the cubic sum of n in the key to value mappings.
*
* Help from : https://www.javatpoint.com/java-map
*
* @author devel
*/
public class Cubic_Mapping_2 {
public static void main(String[] args) {
int j, k, l, i;
double n = 4.0;
double v = Math.pow(n, 3.0);
ArrayList<ArrayList<ArrayList<Integer>>> c = new ArrayList<>();
Map map = new HashMap();
for (j = 0; j <= v-1; j++) {
for (k = 0; k <= n; k++) {
for(l = 0; l <= n; l++) {
for (i = 0; i <= n; i++) {
map.put(c.get(k).get(l).get(i), j);
}
}
}
}
Set set = map.entrySet(); //Converting to Set so that we can traverse
Iterator itr = set.iterator();
while(itr.hasNext()){
//Converting to Map.Entry so that we can get key and value
Map.Entry entry = (Map.Entry) itr.next();
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
`
Thank you, look forward to hearing from you.
R
Tried swapping the key and value variables around, <int, arraylist> and tried calling c variable as an output to check content, however that will not work because there is no content it is a mapping schema.
There does not seem to be any error message on compile in VSCode however in eclipse just a generic exception for 0 index.
################################################################
I wrote a new solution -
`
package javatestfiles;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
/**
* The purpose of the class is to map a cubic calculation to
* a multidimensional ArrayList as the partials iterate over
* the cubic sum of n in the key to value mappings.
*
* Help from : https://www.javatpoint.com/java-map
* https://www.baeldung.com/java-multi-dimensional-arraylist
* https://www.w3schools.com/java/java_hashmap.asp
* https://www.geeksforgeeks.org/java-util-hashmap-in-java-with-examples/
*
* @author devel
*/
public class Cubic_Mapping_3 {
static Map<Integer, Object> map = new HashMap<>();
static ArrayList<ArrayList<ArrayList<Object>>> c;
/**
* Populate an ArrayList with n indices cubed.
*
* @param n
* @return Three dimensional ArrayList c
*/
public static ArrayList<ArrayList<ArrayList<Object>>> three_d_list(int n) {
int i, j;
int x_axis = n;
int y_axis = n;
int z_axis = n;
ArrayList<ArrayList<ArrayList<Object>>> c = new ArrayList<>(x_axis);
for (i = 0; i <= x_axis; i++) {
c.add(new ArrayList<ArrayList<Object>>(z_axis));
for (j = 0; j < z_axis; j++) {
c.get(i).add(new ArrayList<Object>(y_axis));
}
}
return c;
}
/**
* Randomly populate the mapped volumetrics with Neurons
* @param neuron
* @param qty - Quantity of neurons
* @param v - Received autonomously from mapping()
*/
public static void populate(Object neuron, int qty, double v) {
Random rand = new Random();
int i;
for (i = 0; i <= qty; i++) {
map.put(rand.nextInt((int) v), neuron);
}
}
/**
* Maps the cubic index length to the ArrayList c and calls the populate()
* autonomously to populate the list and the mapping with the Neurons
*
* @param neuron - An Objectified ArrayList comprising the AI and areas / proximities
* @param qty - The quantity of Neurons to deploy randomly in your mapped volumetrics
* @param n - The index to be used to calculate the cubic size of the volume
*/
public static void mapping(ArrayList<Object> neuron, int qty, int n) {
int j, k, l, i;
double v = Math.pow(n, 3);
ArrayList<ArrayList<ArrayList<Object>>> c = three_d_list(n);
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
for(l = 0; l < n; l++) {
c.get(j).get(k).add(l);
for (i = 0; i < v; i++) {
map.put(i, c.get(j).get(k).get(l));
}
}
}
}
populate(neuron, qty, v);
}
/**
* Clear the data in memory store after use.
*/
public static void clearall() {
map.clear();
c.clear();
}
public static Map<Integer, Object> main(String[] args) {
mapping(null, 0, 0); //Entry point, the autonomy does the rest.
return map;
}
}
`