List如何进行元素相加

luallen 2011-03-08 08:20:25
class  A
{
private String productname; //产品名称
private String productunit; //产品计量单位
private Int productnum; //产品数量
private String type; //出入库类型 出库/入库

}

List <A> list = new ArrayList<A>();
向list里面添加如下元素

产品名称 产品计量单位 产品数量 出入库类型
A 个 5 出
A 个 2 入
A 个 1 入
B 只 6 入
B 只 2 出
C 个 1 入
C 个 2 入
C 个 3 出
.
.
.
list是按相同产品名称排序的,同一种产品计量单位相同。
怎么对list里面的元素相加减,相同名称的元素入库的减出库的,最后再得到一个list2<A>
产品名称    产品计量单位    产品数量
A 个 -2
B 只 4
C 个 0
...全文
1816 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
asd12121200 2011-03-09
  • 打赏
  • 举报
回复
重写一下方法
i李小武 2011-03-09
  • 打赏
  • 举报
回复
写一个自己的list继承ArrayList(或AbstractList)然后重写add方法。
QQ153984069 2011-03-08
  • 打赏
  • 举报
回复
package org.lxh.demo11.comparabledemo;
import java.util.*;
class Product implements Comparable<Product> //指定类型为Product
{
private String name; //产品名称
private String unit; //产品计量单位
private int num; //产品数量
private String type; //出库/入库
public Product(String name,String unit,int num,String type){
this.name=name;
this.unit=unit;
this.num=num;
this.type=type;
}
public String toString(){
return name+"\t\t"+unit+"\t\t"+num+"\t\t"+type+"\n";
}
public int compareTo(Product pro){//覆写compareTo方法,实现排序规则的应用
if(this.name.compareTo(pro.name)>0){
return 1;
}else if(this.name.compareTo(pro.name)<0){
return -1;
}else{
return 0;
}
}
public void setName(String name){
this.name=name;
}
public void setUnit(String unit){
this.unit=unit;
}
public void setNum(int num){
this.num=num;
}
public void setType(String type){
this.type=type;
}

public String getName(){
return this.name;
}
public String getUnit(){
return this.unit;
}
public int getNum(){
return this.num;
}
public String getType(){
return this.type;
}
}
class ComparableDemo002
{
public static void main(String[] args)
{
Product pro[]={
new Product("A","个",5,"出"),
new Product("A","个",2,"入"),
new Product("A","个",1,"入"),
new Product("B","只",6,"入"),
new Product("B","只",2,"出"),
new Product("C","个",1,"入"),
new Product("C","个",2,"入"),
new Product("C","个",3,"出"),
};
Arrays.sort(pro);
System.out.println(Arrays.toString(pro));

int[] sum=new int[3];
for(int i=0;i<pro.length;i++){
if("A".equals(pro[i].getName())){
if("出".equals(pro[i].getType())){
sum[0]=sum[0]-pro[i].getNum();
}else if("入".equals(pro[i].getType())){
sum[0]=sum[0]+pro[i].getNum();
}
}
if("B".equals(pro[i].getName())){
if("出".equals(pro[i].getType())){
sum[1]=sum[1]-pro[i].getNum();
}else if("入".equals(pro[i].getType())){
sum[1]=sum[1]+pro[i].getNum();
}
}
if("C".equals(pro[i].getName())){
if("出".equals(pro[i].getType())){
sum[2]=sum[2]-pro[i].getNum();
}else if("入".equals(pro[i].getType())){
sum[2]=sum[2]+pro[i].getNum();
}
}
}
System.out.println(Arrays.toString(sum));
}
}
Nodin 2011-03-08
  • 打赏
  • 举报
回复

List<String> result = new ArrayList<String>();
A temp = null;
int count = 0,len = list.size();
for(A a : list){
if(null == temp){
temp = a;
}
if(!a.getProductname().equals(temp.getProductname())){
result.add(temp.getProductname()+" "+temp.getProductunit()+" "+count);
temp = a;
count = 0;
}
count += ("入".equals(a.getType())?1:-1)*a.getProductnum();
}
result.add(temp.getProductname()+" "+temp.getProductunit()+" "+count);
System.out.println(result.toString());
Evil_cloud 2011-03-08
  • 打赏
  • 举报
回复
	 for(A a:list){
boolean isContained=false;
if(a.getType().equals("出")){
a.setProductnum(0-a.getProductnum());
}

for(A aa:list2){
if(aa.getProductname().equals(a.getProductname())){
aa.setProductnum(aa.getProductnum()+a.getProductnum());
isContained=true;
}
}
if(!isContained)
list2.add(a);
}


楼主做个参考吧!希望能帮到你!
yuanyue0540 2011-03-08
  • 打赏
  • 举报
回复
都这么多的答案了,楼主应该得到想要的答案了吧!
sevendawn 2011-03-08
  • 打赏
  • 举报
回复
就是循环判断,相加减
luallen 2011-03-08
  • 打赏
  • 举报
回复
不会用map
luallen 2011-03-08
  • 打赏
  • 举报
回复
能不能帮忙写点代码,新手,最后不用map,我会用map
colachens 2011-03-08
  • 打赏
  • 举报
回复
呵呵,好像是同样的问题哦

循环前定义一个Map吧,把名称当 key

开始循环
当 key 不存在时,直接 put 进 map
当 key 存在时, 先 get 出来,根据 出入加减 操作后 再 put 到map

最后

按 map 的 key排序,输出就可以了

-----------------------------

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.名称())){
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名称(), 0-a.get出入库());
}else{
map.put(a.get名称(), a.get出入库());
}
}//if(map)
}//for
whut_lcy 2011-03-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xiasihua88 的回复:]
循环将名称放入String数组中,并且数组内容不重复(可以搞定的吧)
外层循环String数组,里层循环list,list名称与string数组中相同的相加(可以搞定的吧)
[/Quote]+1
开发无难事 2011-03-08
  • 打赏
  • 举报
回复
循环将名称放入String数组中,并且数组内容不重复(可以搞定的吧)
外层循环String数组,里层循环list,list名称与string数组中相同的相加(可以搞定的吧)
开发无难事 2011-03-08
  • 打赏
  • 举报
回复
循环加判断,搞定
hanRivergo 2011-03-08
  • 打赏
  • 举报
回复
先导入,再导出进行归类,进行加减。
ab159951 2011-03-08
  • 打赏
  • 举报
回复
ding
特醇迎客松 2011-03-08
  • 打赏
  • 举报
回复
djhtj
dadi5566 2011-03-08
  • 打赏
  • 举报
回复
循环遍历集合 判断如有名称相等的元素 就取消该元素的添加 最后得出的集合就是想要的结果
zn85600301 2011-03-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 colachens 的回复:]

呵呵,好像是同样的问题哦

循环前定义一个Map吧,把名称当 key

开始循环
当 key 不存在时,直接 put 进 map
当 key 存在时, 先 get 出来,根据 出入加减 操作后 再 put 到map

最后

按 map 的 key排序,输出就可以了

-----------------------------

Map<String,Integer>……
[/Quote]
以产品名称为key value是一个产品对象
每次put的时间 先判断 是否已经存在 区别出入的值
zqfddqr 2011-03-08
  • 打赏
  • 举报
回复
我也写个一个


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
*
* @author dell
*/
public class NewClass {

List<A> list2 = new ArrayList<A>();

static List<A> test(List<A> list) {
Collections.sort(list);
List<A> list2 = new ArrayList<A>();
A temp = null;
for (A a : list) {
if (temp == null) {
temp = a;
if (temp.getType().equals("出")) {
temp.setProductnum(0 - temp.getProductnum());
}
} else {
if (temp.getProductname().equals(a.getProductname())) {
int t = temp.getProductnum();
if (a.getType().equals("入")) {
t = t + a.getProductnum();
} else {
t = t - a.getProductnum();
}
temp.setProductnum(t);
} else {
list2.add(temp);

temp = a;
if (temp.getType().equals("出")) {
temp.setProductnum(0 - temp.getProductnum());
}
}
}

}
list2.add(temp);
return list2;
}
static List<A> list = new ArrayList<A>();

public static void main(String[] args) {
list.add(new A("A", "个", 5, "出"));
list.add(new A("A", "个", 2, "入"));
list.add(new A("A", "个", 1, "入"));
list.add(new A("B", "只", 6, "入"));
list.add(new A("B", "只", 2, "出"));
list.add(new A("C", "个", 1, "入"));
list.add(new A("C", "个", 2, "入"));
list.add(new A("C", "个", 3, "出"));
List<A> newlist = test(list);
for (A a : newlist) {
System.out.println(a.toString());
}

}
}

class A implements Comparable {

private String productname; //产品名称
private String productunit; //产品计量单位
private Integer productnum; //产品数量
private String type; //出入库类型 出库/入库

public A(String productname, String productunit, Integer productnum, String type) {
this.productname = productname;
this.productunit = productunit;
this.productnum = productnum;
this.type = type;
}

public int compareTo(Object object) {
return this.productname.compareTo(((A) object).productname);
}

public String toString() {
return "productname \t" + productname + "\tproductunit\t " + productunit + "\tproductnum\t " + productnum + "\ttype\t " + type;
}

public String getProductname() {
return productname;
}

public void setProductname(String productname) {
this.productname = productname;
}

public Integer getProductnum() {
return productnum;
}

public void setProductnum(Integer productnum) {
this.productnum = productnum;
}

public String getProductunit() {
return productunit;
}

public void setProductunit(String productunit) {
this.productunit = productunit;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}



62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧