You can provide a unique key for the variable and then store its reference as the value.
In this examples first method we create 9 arrays which we store in a Map, each with different names. In the example below we use a TreeMap.
In the second method we print out the names of each key, and loop through the values of its value object (which is an array).
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
/**
*
* @author
*/
public class Main {
Map
/**
* Example method for creating and storing arrays in a map
*/
public void createArrays() {
for (int i = 1; i <= 9; i++) {
int[] array = new int[3];
array[0] = i;
array[1] = i + 1;
array[2] = i + 2;
map.put(("array_" + i), array);
}
}
/**
* Example method for printing arrays stored in a map
*/
public void printArrays() {
Iterator
while (iter.hasNext()) {
String arrayName = iter.next();
int[] array = map.get(arrayName);
System.out.println("Values for array " + arrayName + ":");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
main.createArrays();
main.printArrays();
}
}
The output of the code above will be:
Values for array array_1:
1
2
3
Values for array array_2:
2
3
4
Values for array array_3:
3
4
5
Values for array array_4:
4
5
6
Values for array array_5:
5
6
7
Values for array array_6:
6
7
8
Values for array array_7:
7
8
9
Values for array array_8:
8
9
10
Values for array array_9:
9
10
11

1 comment:
What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.
Post a Comment