我有一个包含值 {a,b,a,c,d,b,a}
的数组列表
我想对列表中的每个元素进行比较,并将 common indexes
的 pair
插入到 List of array
或使用 java
示例输出:[[0,2,6], [1,4]]
解释:a
位于索引 0,2,6
,b
位于索引 1,4
到目前为止我有这个:
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]
更新:
我的想法是获取 [[0,4],[1,3],[3,1]] 作为 myList
中的元素,但我无法获取。任何帮助深表感谢!谢谢!
基于上面的索引数组,我想在这些索引处比较不同 List
B
和 C
中的元素 - 意思比较 List B 和 C
中索引 0,2,6
处的元素,并检查所有三个元素是否相等。与索引 1,4
最佳答案
我认为这对你有帮助:
ArrayList<int[]> arrayList =new ArrayList<>();
int[] arrayItem={0,2,6};
int[] arrayItem2={1,4};
arrayList.add(arrayItem);
arrayList.add(arrayItem2);
关于java - 如何使用java创建数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55914131/