Karolina Cabaj Karolina Cabaj - 1 year ago 46
Java Question

How to display keys from Map<int[],Double>?

I want to add x elements (just keys) from sorted map to list and display them.

public static ArrayList<int[]> miZrodzicowIpotomstwa(Map<int[],Double> mapVectFunc_tmp, int x)
{
ArrayList<int[]> listaMi = new ArrayList<>();
ArrayList<int[]> klucz_t = new ArrayList<>(mapVectFunc_tmp.keySet());

for(int i=0; i<x; i++)
{
listaMi.add(klucz_t.get(i));
}
return listaMi;
}


in main:

Map<int[],Double> mapVectFunc = new LinkedHashMap<int[],Double>();
int []t1={1,0,0,1};
int []t2={1,1,0,0};
int []t3={1,0,1,1};
mapVectFunc.put(t1,26.0);
mapVectFunc.put(t2,1.0);
mapVectFunc.put(t3,6767.0);
ArrayList<int[]> ll= miZrodzicowIpotomstwa(mapVectFunc, 2);
System.out.printf("\n list of two keys: "+ll);


I received the following values(not arrays) :


list of two keys: [[[email protected], [[email protected]]


Does anybody has an idea how to convert it into arrays?

Answer Source

You can print them like this.

public static void main(String[] args) {
    Map<int[], Double> mapVectFunc = new LinkedHashMap<int[], Double>();
    int[] t1 = { 1, 0, 0, 1 };
    int[] t2 = { 1, 1, 0, 0 };
    int[] t3 = { 1, 0, 1, 1 };
    mapVectFunc.put(t1, 26.0);
    mapVectFunc.put(t2, 1.0);
    mapVectFunc.put(t3, 6767.0);
    ArrayList<int[]> ll = miZrodzicowIpotomstwa(mapVectFunc, 2);

    int c = 0;
    for (int[] i : ll) {
        System.out.println("Array" + c++);
        for (int j : i) {
            System.out.println(j);
        }
    }   
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download