I'm trying to use the code below to create a multidimensional ArrayList. My code populates the inner ArrayList (localSolutions) just fine, but when I try an add that ArrayList to the outer ArrayList (solutions), something goes wrong, and it adds empty ArrayLists instead.
public class MathCapstone {
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> list = entireList(10);
for(int q = 0;q<list.size();q++) {
System.out.println(list.get(q));
}
public static ArrayList<ArrayList<Integer>> entireList(int max) {
ArrayList<ArrayList<Integer>> solutions = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> localSolutions = new ArrayList<Integer>();
for(int i = 1; i <= max; i++) {
for(int j = 1; j < i; j++) {
//System.out.println(j + "mod" + i + "=" + (j*j)%i);
if ((j*j)%i == 1) {
localSolutions.add(j);
}
}
//System.out.println(localSolutions.toString());
solutions.add(localSolutions);
localSolutions.clear();
}
return solutions;
}
On a final note: would it be better to use a HashMap of ArrayLists (eventually I'm going to be creating CDF's for max values up to about 10k)?
HashMap<Integer, Whatever>as opposed to anArrayList<Whatever>if the indexes you want to use are all numbers between 0 and something, and you'll lose a little performance. HashMaps are better if you need non-integer keys, or if the distribution of the indices is sparse.List<List<Integer>> solutions = new ArrayList<List<Integer>>().