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
14.7k8 gold badges55 silver badges71 bronze badges
4 Answers 4
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);
}
}
}
Sign up to request clarification or add additional context in comments.
Comments
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
answered May 22, 2017 at 16:43
Vasyl Lyashkevych
2,2682 gold badges28 silver badges41 bronze badges
2 Comments
Karolina Cabaj
The problem is that keys are arrays. Result: [I@54bedef2 26.0 [I@5caf905d 1.0 [I@27716f4 6767.0
Vasyl Lyashkevych
Thanks, I am going to check
for (int[] i : l1){
for(int j:i){
System.out.println(j);
}
}
2 Comments
Karolina Cabaj
i received the same : [[I@54bedef2, [I@5caf905d]
Binu
for (int[] i : l1){ for(int j:i){ System.out.println(j); } }
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;
}
answered May 22, 2017 at 17:22
gati sahu
2,6512 gold badges12 silver badges16 bronze badges
Comments
lang-java
int, and use them as keys into the map. Observe how three items are added, then come up with a different strategy.