0

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: [[I@54bedef2, [I@5caf905d]

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

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
  • Possible duplicate of [How to convert int\[\] into List in Java?](https://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java) – zeppelin May 22 '17 at 16:35
  • 2
    Display issue is just the tip of the iceberg here. You have a much bigger issue here: the map does not work. To see why, create three identical arrays of `int`, and use them as keys into the map. Observe how three items are added, then come up with a different strategy. – Sergey Kalinichenko May 22 '17 at 16:35

4 Answers4

0
for (int[] i : l1){
    for(int j:i){
        System.out.println(j);
    }
}
Binu
  • 754
  • 6
  • 15
0
ArrayList<int[]> ll= miZrodzicowIpotomstwa(mapVectFunc, 2);
...
System.out.println("\n list of two keys: ");
for (int[] i: ll.keySet()){

            String key =i.toString();
            String value = ll.get(i).toString();  
            System.out.println(key + " " + value);  
}

A little bit more here: Printing HashMap In Java

Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
0

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);
        }
    }   
}
Piyush
  • 1,162
  • 9
  • 17
0

The problem is key in hash map.Please try with below code

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

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

        for (Integer[] arr : mapVectFunc_tmp.keySet()) {
            ;
            klucz_t.addAll(Arrays.asList(arr));
        }
        return klucz_t;
    }
gati sahu
  • 2,576
  • 2
  • 10
  • 16