Reputation: 1395
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
Upvotes: 4
Views: 258
Reputation: 7185
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]]
Upvotes: 0
Reputation: 282
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.
Upvotes: 1
Reputation: 362
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);
Upvotes: 2