4

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

4
  • stackoverflow.com/a/8321984/10747672 Commented Apr 30, 2019 at 4:49
  • use Comparator its in built in java, Kinldy have a look Commented Apr 30, 2019 at 4:52
  • You make a new ArrayList like List<int[]> bList = new ArrayList<int[]>(); Commented Apr 30, 2019 at 4:52
  • @AdanVivero I updated the questions. Please check and Thanks for the response Commented Apr 30, 2019 at 5:45

3 Answers 3

2

I think this will help you:

    ArrayList<int[]> arrayList =new ArrayList<>();
    int[] arrayItem={0,2,6};
    int[] arrayItem2={1,4};
    arrayList.add(arrayItem);
    arrayList.add(arrayItem2);
Sign up to request clarification or add additional context in comments.

Comments

1

Homework done, check this:

package com.company;

import java.util.*;

public class Main {

    public static void main(String[] args) {
    // write your code here
        HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        List<String> letter = 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 (letter.get(i).equals(letter.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();
            int[] intValues = new int[2];
            intValues[0] = key;
            intValues[1] = value;
            myList.add(intValues);
        }

        String toPrint = new String();
        toPrint = toPrint.concat("[");
        for(int k = 0; k < myList.size(); k++) {
            toPrint = toPrint.concat("[");
            for(int l = 0; l < myList.get(k).length; l++) {
                toPrint = toPrint.concat(String.valueOf(myList.get(k)[l]));
                if(l != (myList.get(k).length-1)){
                    toPrint = toPrint.concat(",");
                }
            }
            toPrint = toPrint.concat("]");
        }
        toPrint = toPrint.concat("]");

        System.out.println(toPrint);
        //[[0,4][1,3][3,1]]
    }
}

If all you needed to do was print the values on the sceen this code will work for you.

1 Comment

I updated the questions. Please check and Thanks for the response
0

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]]

Comments

Your Answer

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.