Creating and storing arrays in a map


Sometimes you want to create variable names dynamically, for example in a loop. One way of doing this is to not create the variables with different names but rather to store them in a Collection object that takes a key-value pair.
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 javadb.com
 */

public class Main {
    
    Map<String, int[]> map = new TreeMap<String, int[]>();
    
    /**
     * 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<String> iter = map.keySet().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

Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: