不明白你1 2 6和下面的关系,你是要将10,10,20,20,11,10,20,9,29里面相同数得下标放在一起呢,还是?
如果只是要index,那还不简单?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Cat {
public static void main(String[] args) {
int[] ary = {10,10,20,20,11,10,20,9,29};
Map
for(int i = 0, len = ary.length; i < len; i++){
if(map.containsKey(ary[i])){
map.get(ary[i]).add(i+1);
}else{
List
list.add(i+1);
map.put(ary[i], list);
}
}
Iterator ite = map.keySet().iterator();
while(ite.hasNext()){
Integer key = (Integer) ite.next();
System.out.print("Item " + key.intValue() + " index are: ");
for(int value: map.get(key)){
System.out.print(value + " ");
}
System.out.println();
}
}
}
----------------------
Item 29 index are: 9
Item 11 index are: 5
Item 10 index are: 1 2 6
Item 9 index are: 8
Item 20 index are: 3 4 7
java.io.BufferedReader;
import java.io.FileReader;
public class ReadText {
public static void main(String[] args) throws Exception {
//从文件中取出所有数据
FileReader fin = new FileReader("number.txt");
BufferedReader bf = new BufferedReader(fin);
String str = "";
String data = "";
while ((str = bf.readLine()) != null) {
data += str;
}
//将字符串按空格分开,\\s+表示一个或多个空格(正则表达式,查看API Pattern类)
String[] datas = data.split("\\s+");
int[] intdata = new int[datas.length];
int j = 0;
for (int i = 0; i < datas.length; i++) {
String d = datas[i];
if (d.matches("\\d+")) { //如果d是数字(\\d+正则表达式中表示为数据)
int n = Integer.parseInt(d);
if (n > 4) {
intdata[j++] = n;
}
}
}
for (int i = 0; i < intdata.length; i++) {
if (intdata[i] != 0)
System.out.println(intdata[i]);
}
}
}