50,344
社区成员




import java.util.Arrays;
public class ArryCombTest {
public static void main(String[] args) {
int sizeOfDifGoods = 0;
Goods[] goodsAll = new Goods[]{new Goods(6.35f, 20),new Goods(6.37f, 100),
new Goods(6.37f, 200),new Goods(6.35f, 60),new Goods(6.35f, 40),
new Goods(6.36f, 50),new Goods(6.4f, 10),new Goods(6.4f, 20),
new Goods(6.4f, 40),};
//由于数组长度不可变,第一个数组必须和原数组一样长
Goods[] goodsStics = new Goods[goodsAll.length];
//第一个无需比较直接加入
goodsStics[0] = goodsAll[0];
sizeOfDifGoods++;
//依次比较加入
for(int i = 1; i < goodsAll.length; i++) {
boolean theSame = false;
int theSameIndex = 0;
for (int j = 0; j < goodsStics.length; j++) {
if(goodsAll[i].equals(goodsStics[j])){
theSame = true;
theSameIndex = j;
break;
}
}
if (theSame) {
goodsStics[theSameIndex].mergeSameSale(goodsAll[i]);
}
else {
goodsStics[sizeOfDifGoods] = goodsAll[i];
sizeOfDifGoods++;
}
}
//新对象数组放不重复元素,去除null元素
Goods[] goodsSticsFinal = new Goods[sizeOfDifGoods];
for (int i = 0; i < goodsSticsFinal.length; i++) {
goodsSticsFinal[i] = goodsStics[i];
}
//打印结果试试
System.out.println(Arrays.asList(goodsAll));
System.out.println(Arrays.asList(goodsSticsFinal));
}
}
class Goods{
private float price;
private int sale;
public Goods(float price,int sale){
this.price = price;
this.sale = sale;
}
@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof Goods &&
Math.abs((this.price - ((Goods)obj).price)) < 0.001f) {
return true;
}
return false;
}
public void mergeSameSale(Goods goods){
this.sale += goods.sale;
}
@Override
public String toString() {
return ""+price+" "+sale;
}
}
import java.util.Arrays; public class ArryCombTest { public static void main(String[] args) { int sizeOfDifGoods = 0; Goods[] goodsAll = new Goods[]{new Goods(6.35f, 20),new Goods(6.37f, 100), new Goods(6.37f, 200),new Goods(6.35f, 60),new Goods(6.35f, 40), new Goods(6.36f, 50),new Goods(6.4f, 10),new Goods(6.4f, 20), new Goods(6.4f, 40),}; //由于数组长度不可变,第一个数组必须和原数组一样长 Goods[] goodsStics = new Goods[goodsAll.length]; //第一个无需比较直接加入 goodsStics[0] = goodsAll[0]; sizeOfDifGoods++; //依次比较加入 for(int i = 1; i < goodsAll.length; i++) { boolean theSame = false; int theSameIndex = 0; for (int j = 0; j < goodsStics.length; j++) { if(goodsAll[i].equals(goodsStics[j])){ theSame = true; theSameIndex = j; break; } } if (theSame) { goodsStics[theSameIndex].mergeSameSale(goodsAll[i]); } else { goodsStics[sizeOfDifGoods] = goodsAll[i]; sizeOfDifGoods++; } } //新对象数组放不重复元素,去除null元素 Goods[] goodsSticsFinal = new Goods[sizeOfDifGoods]; for (int i = 0; i < goodsSticsFinal.length; i++) { goodsSticsFinal[i] = goodsStics[i]; } //打印结果试试 System.out.println(Arrays.asList(goodsAll)); System.out.println(Arrays.asList(goodsSticsFinal)); } } class Goods{ private float price; private int sale; public Goods(float price,int sale){ this.price = price; this.sale = sale; } @Override public boolean equals(Object obj) { if (obj != null && obj instanceof Goods && Math.abs((this.price - ((Goods)obj).price)) < 0.001f) { return true; } return false; } public void mergeSameSale(Goods goods){ this.sale += goods.sale; } @Override public String toString() { return ""+price+" "+sale; } }