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
14.7k8 gold badges55 silver badges71 bronze badges
asked May 22, 2017 at 16:31
2
  • Possible duplicate of How to convert int[] into List<Integer> in Java? Commented May 22, 2017 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. Commented May 22, 2017 at 16:35

4 Answers 4

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);
 }
 } 
}
answered May 22, 2017 at 16:43
Sign up to request clarification or add additional context in comments.

Comments

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

answered May 22, 2017 at 16:43

2 Comments

The problem is that keys are arrays. Result: [I@54bedef2 26.0 [I@5caf905d 1.0 [I@27716f4 6767.0
Thanks, I am going to check
0
for (int[] i : l1){
 for(int j:i){
 System.out.println(j);
 }
}
answered May 22, 2017 at 16:37

2 Comments

i received the same : [[I@54bedef2, [I@5caf905d]
for (int[] i : l1){ for(int j:i){ System.out.println(j); } }
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;
 }
answered May 22, 2017 at 17:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.