3 回答

TA貢獻1772條經(jīng)驗 獲得超6個贊
我認為這會幫助你:
ArrayList<int[]> arrayList =new ArrayList<>();
int[] arrayItem={0,2,6};
int[] arrayItem2={1,4};
arrayList.add(arrayItem);
arrayList.add(arrayItem2);

TA貢獻1776條經(jīng)驗 獲得超12個贊
功課做完了,看看這個:
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]]
}
}
如果您需要做的只是在屏幕上打印值,則此代碼將為您工作。

TA貢獻1906條經(jīng)驗 獲得超3個贊
您不能使用Map以前檢查 String 的所有索引的方式。您可以嘗試ArrayList如下,
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;
}
}
輸出/輸出:
[[0, 2, 6], [1, 5]]
添加回答
舉報