日期:2014-05-20 浏览次数:20614 次
public static void main(String[] args) {
int [] arr = {1,2,2,1,4,3,3,4};
HashMap<Integer,Integer> m =new HashMap<Integer,Integer>();
int count=1;
for (int i = 0; i < arr.length; i++) {
int tep=arr[i];
for (int j = i+1; j < arr.length; j++) {
if(tep!=arr[j]){
}else{
count ++;
m.put(j, arr[j]*count);
}
}
count=1;
}
Iterator<Integer> iter = m.keySet().iterator();
while (iter.hasNext()) {
int key = iter.next();
int value = m.get(key);
arr[key] = value;
}
System.out.println(m);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i] + " ");
}
}
int[] arr = { 1, 2, 2, 1, 4, 3, 3, 4, 3 };
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
// int count = 1;
// 用来记录每个数字出现的次数
HashMap<Integer, Integer> repeatCount = new HashMap<Integer, Integer>();
for (int i = 0; i < arr.length; i++) {
int tep = arr[i];
int count = 1;
// 判定是否存在
if (repeatCount.containsKey(tep)) {
count = repeatCount.get(tep) + 1;
repeatCount.put(tep, count);
} else {
repeatCount.put(tep, 1);
}
System.out.print(tep * count);
}
public static void main(String[] args) {
int [] arr = {1,2,2,2,4,3,3,4,3};
HashMap<Integer,Integer> m =new HashMap<Integer,Integer>();
int count=1;
m.put(0,arr[0]);
for (int i = 1; i < arr.length; i++) {
int tep=arr[i];
for (int j = 0; j < i; j++) {
if(tep!=arr[j])
{ }
else{
count ++;
m.put(i, arr[i]*count);
}
}
count=1;
}
Iterator<Integer> iter = m.keySet().iterator();
while (iter.hasNext()) {
int key = iter.next();
int value = m.get(key);
arr[key] = value;
}
System.out.println(m);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
int [] arr = {1,2,2,1,4,3,3,4,3};//{1,2,2,1,4,3,3,4,3,3,3};
int[] num = new int[arr.length];
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
for(int i = 0 ; i < arr.length ; i++){
if( m.get(arr[i]) == null){