class A
{
private String 姓名;
private Int 年龄;
private String 类型;
}
List <A> list = new ArrayList<A>();
现在list里面假设有如下元素(已经按姓名排序)
怎么遍历list最后得出一个
姓名 年龄 类型
A 12 加
A 2 减
A 13 减
B 10 加
B 2 减
C 8 加
姓名 年龄
A -3
B 8
C 8
什么意义??
把list里面 姓名相同的 年龄按照类型 相加 或 相减。
开始循环
当 key 不存在时,直接 put 进 map
当 key 存在时, 先 get 出来,根据 加或减 操作后 再 put 到map
最后
按 map 的 key排序,输出就可以了
基础不是很好 能不能帮忙写两行
A temp = null;
String name = list.get(0).get姓名();
int len = list.size(),ageResult = 0;
for(int i = 0;i < len;i++){
temp = list.get(i);
if(temp.get姓名().equals(name)){
ageResult += (temp.get类型().equals("加")?1:-1)*temp.get年龄();
}else{
System.out.println(name+":"+ageResult);
name = temp.get姓名();
ageResult = temp.get年龄();
}
}
System.out.println(name+":"+ageResult);
list.add(new A("A", 12, "加"));
list.add(new A("A", 2, "减"));
list.add(new A("A", 13, "减"));
list.add(new A("B", 10, "加"));
list.add(new A("B", 2, "减"));
list.add(new A("C", 8, "加"));
Map<String,Integer> uniqueNameMap = new HashMap<String,Integer>();
for(A a : list)
{
int leftAgeInt = 0;
String name = a.get姓名();
if(uniqueNameMap.containsKey(name))
{
Integer leftAge = (Integer)uniqueNameMap.get(name);
leftAgeInt = leftAge.intValue();
}
String type = a.get类型();
if("加".equals(type))
{
leftAgeInt += a.get年龄();
}
else if("减".equals(type))
{
leftAgeInt -= a.get年龄();
}
uniqueNameMap.put(name, leftAgeInt);
}
System.out.println(uniqueNameMap);
public class A {
private String name;
private int age;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public A(String name, int age, String type) {
this.name = name;
this.age = age;
this.type = type;
}
}
public class Test {
public static void main(String[] args) {
List<A> list = new ArrayList<A>();
list.add(new A("A", 12, "add"));
list.add(new A("A", 2, "sub"));
list.add(new A("A", 13, "sub"));
list.add(new A("B", 10, "add"));
list.add(new A("B", 2, "sub"));
list.add(new A("C", 8, "add"));
int countA = 0;
int countB = 0;
int countC = 0;
for (A a : list) {
if ("add".equals(a.getType())) {
if ("A".equals(a.getName())) {
countA += a.getAge();
} else if ("B".equals(a.getName())) {
countB += a.getAge();
} else if ("C".equals(a.getName())) {
countC += a.getAge();
}
} else {
if ("A".equals(a.getName())) {
countA -= a.getAge();
} else if ("B".equals(a.getName())) {
countB -= a.getAge();
} else if ("C".equals(a.getName())) {
countC -= a.getAge();
}
}
}
System.out.println("姓名 年龄");
System.out.println("A " + countA);
System.out.println("B " + countB);
System.out.println("C " + countC);
}
}
姓名 年龄
A -3
B 8
C 8
Map<String,Integer> map = new HaspMap<String,Integer>();
for(int i=0;i<list.size();i++){
A a = list.get(i);
if(map.containsKey(a.get姓名())){
if("加".equals(a.get类型())){
map.put(a.get姓名(), map.get(a.get姓名())+a.get年龄());
}else{
map.put(a.get姓名(), map.get(a.get姓名())-a.get年龄());
}
}else{
if("加".equals(a.get类型())){
map.put(a.get姓名(), a.get年龄());
}else{
map.put(a.get姓名(), -a.get年龄());
}
}
}
求助:C++ typedef void * HCONN 转成intptr报内存错误 Python源码阅读 新人求教,怎么在JSTL中叠加给一个变量赋值? C语言文件结束指示符EOF 无意间在csdn发现了我用C#写的第一个程序,好激动~~~ 关于调用js文件里的内容 干扰对消方法在扩频系统中的应用 求一个mips版的coredll.dll 【轉帖】媽媽,我等了您二十年 CFile::open打开文件时,出现access to an unnamed file was denied 该继续做技术还是转行?过来人看看吧…… 最近在看spring代码。求Spring类图,UML图。比如事务管理涉及的类的类图,bean扫描加载初始化的类图 有没有一篇完整的部署文档 求段js代码——在线等 关于winphone引用web service的安全问题,请大家帮帮忙,先谢谢了 求助 c# System.NullReferenceException: 未将对象引用设置到对象的实例 DirectX开发3D应用的问题,求大神~~ jsp引用自定义标签路径错误~(分少在线等) MAC os下Qt的Tab键问题 SQLSERVER2008 死锁问题 由丢失准考证号而解决方法的可能性猜想 JQuery添加div移除div 为用servlet判断前台输入的用户名是否可用,提示信息无法在页面显示? 求HP WebInspect 的使用及分析手册 shh 总是报空指针异常 我的applicationcontext.xml都配好了,可还是报错 求指教啦! 求sql语句,在线等 ubuntu 12.04 安装deb包出现内部错误解决办法 怎么获取积分啊 asp.net 的web.config与iis 的目录安全性的关系 求助,C语言中如何使用fwrite换行写入数据到文本中去?