第1个回答 2010-03-26
public static void main(String[] args) {
Object[] obj = {"a1,b1,c1,100","a1,b2,c1,300","a1,b1,c1,50","a1,b1,c2,100"
,"a1,b2,c1,100"};
MultiMap map = new MultiHashMap();
for (int i = 0; i < obj.length; i++) {
String temp1 = ((String)obj[i]).substring(0, ((String)obj[i]).lastIndexOf(","));
String temp2 = ((String)obj[i]).substring(((String)obj[i]).lastIndexOf(",")+1);
map.put(temp1, temp2);
}
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
List list = (List)map.get(key);
int sum = 0;
for (int i = 0; i < list.size(); i++) {
sum += Integer.parseInt((String)list.get(i));
}
System.out.println(key+":"+sum);
}
看看,不过在运行这前要导一个包 commons-collections.jar
}
第2个回答 2010-03-26
import java.util.*;
public class Test3 {
TestObj testObj = null;
TestObj tempObj = null;
public static void main(String[] args) {
Test3 test = new Test3();
List<TestObj> list = test.getArrayList();
List<TestObj> newList = new ArrayList<TestObj>();
int size_i = list.size();
int size_j = list.size();
for (int i = 0; i < size_i; i++) {
double temp = 0;
if(list.get(i).flag == true){
for (int j = 0; j < size_j; j++) {
if(list.get(j).flag == true){
if ((list.get(i).a == list.get(j).a)
&& (list.get(i).b == list.get(j).b)
&& (list.get(i).c == list.get(j).c)) {
temp += list.get(j).d;
list.get(j).flag = false;
}
}
}
test.testObj = new TestObj(list.get(i).a, list.get(i).b, list
.get(i).c, temp);
newList.add(test.testObj);
list.get(i).flag = false;
}
}
for (int z = 0; z < newList.size(); z++) {
System.out.println(newList.get(z).a + " " + newList.get(z).b + " "
+ newList.get(z).c + " " + newList.get(z).d);
}
//System.out.println(3.4 + 2.8 + 3.1);
}
/**
* @return 返回一个符合要求的List
*/
public List<TestObj> getArrayList() {
List<TestObj> list = new ArrayList<TestObj>();
TestObj t1 = new TestObj("a3", "b1", "c1", 3.4);
TestObj t2 = new TestObj("a1", "b1", "c1", 2.8);
TestObj t3 = new TestObj("a2", "b2", "c2", 1.6);
TestObj t4 = new TestObj("a2", "b2", "c2", 7.2);
TestObj t5 = new TestObj("a1", "b1", "c1", 3.1);
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
list.add(t5);
return list;
}
}
/**
* 数组元素类
*
* @author tenglian
*
*/
class TestObj {
String a;
String b;
String c;
double d;
boolean flag = true;
public TestObj(String a, String b, String c, double d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
第3个回答 2010-03-26
额。。。前面都写好了,我就不费脑筋了
介绍你一个String类的方法,split(",");
可以将一个字符串分割成一个String数组,以指定字符分割
你这里可以用到
第4个回答 2010-03-26
package com;
public class Test {
public static void main(String[] args) throws Exception {
Object[] obj = { "a1,b1,c1,100", "a1,b2,c1,300", "a1,b1,c1,50",
"a1,b1,c2,100", "a1,b2,c1,100" };
Object[] obj1 = new Test().getAll(obj);
for (int i = 0; i < obj1.length; i++) {
if (null != obj1[i]) {
System.out.println(obj1[i]);
}
}
}
public Object[] getAll(Object[] obj) {
Object[] result = new Object[obj.length];
int size = 0;
for (Object o : obj) {
String ys = o.toString();
String key = ys.substring(0, ys.lastIndexOf(','));
boolean add = true;
for (int i = 0; i < size; i++) {
if (result[i].toString().indexOf(key) == 0) {
double v1 = Double.parseDouble(ys.substring(ys
.lastIndexOf(',') + 1));
double v2 = Double.parseDouble(result[i].toString()
.substring(ys.lastIndexOf(',') + 1));
result[i] = key + "," + (v1 + v2);
add = false;
break;
}
}
if (add) {
result[size++] = o;
}
}
return result;
}
}