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

解决方案 »

  1.   

    循环将名称放入String数组中,并且数组内容不重复(可以搞定的吧)
    外层循环String数组,里层循环list,list名称与string数组中相同的相加(可以搞定的吧)
      

  2.   

    呵呵,好像是同样的问题哦循环前定义一个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
      

  3.   

    能不能帮忙写点代码,新手,最后不用map,我会用map
      

  4.   

     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);
     }楼主做个参考吧!希望能帮到你!
      

  5.   


    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());
      

  6.   

    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));
    }
    }
      

  7.   

    我也写个一个 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;
        }
    }
      

  8.   

    以产品名称为key value是一个产品对象
    每次put的时间 先判断 是否已经存在 区别出入的值
      

  9.   

    写一个自己的list继承ArrayList(或AbstractList)然后重写add方法。