有两个eatList1 和eatList2这样的数组,数组里面存着Eatery对象,我想实现的是比较两个数组,把id相同数量相加,不同的放在eatList1数组里,例子如下:
Class Eatery {
public Eatery(int id,int num)
{
this.id =id;
this.num=num;
}
int id;
int num;
public int getNum()
{
return num;
}public void setNum(int num)
{
this.num = num;
}public int getId()
{
return id;
}public void setId(int id)
{
this.id = id;
}
}public static void main(String[]){
Eatery e1 = new Eatery(1,10);
Eatery e2 = new Eatery(2,20);
Eatery e3 = new Eatery(3,30);
Eatery e4 = new Eatery(1,40);eatList1=new ArrayList();
eatList1.add(e1);
eatList1.add(e2);
List eatList2=new ArrayList();
eatList2.add(e3);
eatList2.add(e4);我的算法是:
for(int i=0;i<eatList1.size();i++)
{
for(int j=0;j<eatList2.size();j++)
{
//id相同,把num加到一起
if(((Eatery)eatList1.get(i)).getId()==((Eatery)eatList2.get(j)).getId())
{
int result = ((Eatery)eatList1.get(i)).getNum()+((Eatery)eatList2.get(j)).getNum();
((Eatery)eatList1.get(i)).setNum(result);
}else
{
Eatery e = (Eatery)eatList2.get(j);
if(!eatList1.contains(e)){
eatList1.add(e);
}
}
}
}for(int i=0;i<eatList1.size();i++)
{
System.out.println(eatList1.get(i));
}
}
得到的结果是
id=1,num=50
id=2,num=20
id=3,num=60
id=1,num=80
这个结果是不对的
我想得到的结果是:
id=1 num=40
id=2 num=20
id=3 num=30
Class Eatery {
public Eatery(int id,int num)
{
this.id =id;
this.num=num;
}
int id;
int num;
public int getNum()
{
return num;
}public void setNum(int num)
{
this.num = num;
}public int getId()
{
return id;
}public void setId(int id)
{
this.id = id;
}
}public static void main(String[]){
Eatery e1 = new Eatery(1,10);
Eatery e2 = new Eatery(2,20);
Eatery e3 = new Eatery(3,30);
Eatery e4 = new Eatery(1,40);eatList1=new ArrayList();
eatList1.add(e1);
eatList1.add(e2);
List eatList2=new ArrayList();
eatList2.add(e3);
eatList2.add(e4);我的算法是:
for(int i=0;i<eatList1.size();i++)
{
for(int j=0;j<eatList2.size();j++)
{
//id相同,把num加到一起
if(((Eatery)eatList1.get(i)).getId()==((Eatery)eatList2.get(j)).getId())
{
int result = ((Eatery)eatList1.get(i)).getNum()+((Eatery)eatList2.get(j)).getNum();
((Eatery)eatList1.get(i)).setNum(result);
}else
{
Eatery e = (Eatery)eatList2.get(j);
if(!eatList1.contains(e)){
eatList1.add(e);
}
}
}
}for(int i=0;i<eatList1.size();i++)
{
System.out.println(eatList1.get(i));
}
}
得到的结果是
id=1,num=50
id=2,num=20
id=3,num=60
id=1,num=80
这个结果是不对的
我想得到的结果是:
id=1 num=40
id=2 num=20
id=3 num=30
id=1 num=50
id=2 num=20
id=3 num=30
从你的循环可以看出:
第一次:
eatList1: (1 50) (2 20) (3 30)
第二次:
eatList1: (1 50) (2 20) (3 30) (1 40)
第三次:
eatList1: (1 50) (2 20) (3 60) (1 40)
第四次:
eatList1: (1 50) (2 20) (3 60) (1 80)
也就是你所显示的错误的结果问题出在你在嵌套循环的处理过程中由于采用了eatList1保存对象数组,导致在判断的时候其动态的增长。建议new一个新的list数组来进行保存操作后的数组对象。
这样eatList1不会重复id的数组对象.
id=1 num=50
id=2 num=20
id=3 num=30
我知道是我的else那里有问题,但是我不知道怎么改,像一数说的那样,把else里的数据放在一个新的数组里,我试过了,但是一样是不对的,二楼的回答也是不正确的,各位请指点!
public class Eatery {
public Eatery(int id,int num)
{
this.id =id;
this.num=num;
}
int id;
int num;
public int getNum()
{
return num;
} public void setNum(int num)
{
this.num = num;
} public int getId()
{
return id;
} public void setId(int id)
{
this.id = id;
}
public boolean equals(Object o)
{
if (!( o instanceof Eatery))
{
return false;
}
Eatery ee = (Eatery)o;
if(ee.getId()==this.getId())
{
return true;
}
return false;
}
public int hashCode()
{
return id;
}public static void main(String[]args)
{
Eatery e1 = new Eatery(1,10);
Eatery e2 = new Eatery(2,20);
Eatery e3 = new Eatery(3,30);
Eatery e4 = new Eatery(1,40); List eatList1=new ArrayList();
eatList1.add(e1);
eatList1.add(e2);
List eatList2=new ArrayList();
eatList2.add(e3);
eatList2.add(e4);
List data = new ArrayList();
data.addAll(eatList1);
data.addAll(eatList2);
eatList2 = new ArrayList();
eatList1 = new ArrayList();boolean flag = false;while(true)
{
if(data.size()==0)
{
break;
}
flag =false;
int num = 0;
Eatery ei = (Eatery)data.get(0);
num = ei.getNum();
for(int j=1;j<data.size();j++)
{
Eatery ej = (Eatery)data.get(j);
if(ei.getId()==ej.getId())
{
num = num + ej.getNum();
flag = true;
}
}
ei.setNum(num);
if(flag )
{
eatList1.add(ei);
}
else
{
eatList2.add(ei);
}
data.remove(ei);//根据ID删除所有的相同的对像.
}
for(int i=0;i <eatList1.size();i++)
{
Eatery e = (Eatery)eatList1.get(i);
System.out.println(e.getId()+","+e.getNum());
}
}
}
{
Eatery ej = (Eatery)data.get(j);
if(ei.getId()==ej.getId())
{
num = num + ej.getNum();
data.remove(j);
j--;
flag = true;
}
}这样好像可以了.帮忙测试一下吧...