How to create a List of arrays using java

user6348718 :

I have a arrayList with values {a,b,a,c,d,b,a} I want to make a comparison of each element in the list and insert the pair of common indexes into a List of array or something using java

example output: [[0,2,6], [1,4]] explanation: a is at indexes 0,2,6 and b is at indexes 1,4 So far I have this:

    HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        List<String> name = new ArrayList<String>();
        letter.add("a");
        letter.add("b");
        letter.add("c");
        letter.add("b");
        letter.add("a");

         for (int i = 0; i < letter.size(); i++) {
            for (int j = 1; j < letter.size(); j++) {
                if (letters.get(i).equals(letters.get(j)) && i != j) {
                    hashMap.put(i, j);
                }
            }
        }
        System.out.println(hashMap); //o/p: {0=4, 1=3, 3=1}
        List<int[]> myList = new ArrayList<int[]>();
        Iterator entries = hashMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            Integer key = (Integer)entry.getKey();
            Integer value = (Integer)entry.getValue();
            myList.add(new int[] {key,hashMap.get(key)});
        }
        System.out.println(myList.toString()); 
        //O/P: [[I@380fb434, [I@668bc3d5, [I@3cda1055]

UPDATE: the idea was to get [[0,4],[1,3],[3,1]] as elements in myList but I am not able to get that. Any help is much appreciated! Thanks!

Based on the above array of indexes, I want to compare the elements in a different List B and C at those indexes - meaning compare elements at indexes 0,2,6 in List B and C and check if all three elements are equal. Same for elements at index 1,4

Code_Mode :

You can not use Map the way you used to check all indexes of String. You can try with ArrayList as below,

import java.util.*;

public class ListCharIndexes {
    public static void main(String[] args) {
        List<String> letter = Arrays.asList("a","b","a","c","d","b","a");
       //letter= Arrays.asList("a","b","c","b","a");
        List<List<Integer>> result=new ArrayList<>();
        Set<String> result1=new HashSet<>();
        for (int i = 0; i < letter.size(); i++) {
            if(result1.add(letter.get(i))){ //skip String if it is already processed
                List<Integer> indexes=indexOfAll(letter.get(i), letter);
                if(indexes.size()>1)     //add only pairs
                    result.add(indexes);
            }
        }
        System.out.println(result);
    }
    static List<Integer> indexOfAll(String obj, List<String> list) {
        final List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++)
            if (obj.equals(list.get(i))) 
                indexList.add(i);
        return indexList;
    }
}

O/P:

[[0, 2, 6], [1, 5]]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=68767&siteId=1