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: Social bookmarks del.icio.us digg Furl YahooMyWeb blinkbits BlinkList blogmarks co.mments connoteas De.lirio.us Fark feedmelinks LinkaGoGo Ma.gnolia NewsVine Netvouz RawSugar Reddit scuttle Shadows Simpy Smarking Spurl TailRank Wists segnalo




   Need Java help? You are just a few bucks away.

   This is how it works:
  • Send us a detailed description of what you need help with.
  • We send you a confirmation when we start working on your request, and then we send you the compiled Java code for you to test out.
  • If you are satisfied with the result we send you a secure link where you can make the payment for the Java source code, usually a few bucks (depends on the work required).
  • The source code is sent to you once the payment is completed.


Write your detailed request here:
   

E-mail address: