如下,一段时期内的商品价格于相应卖出量的对象数组
日期 当日价格(元) 卖出量(公斤)
1月1日 6.35 20
1月2日 6.37 100
1月3日 6.37 200
1月4日 6.35 60
1月5日 6.35 40
1月6日 6.36 50
1月7日 6.4 10
1月8日 6.4 20
1月9日 6.4 40
统计这段时期内各价位的卖出量生成一个新的数组,如下(去除重复现象):日期一栏可以不要,目的就是统计各价位总卖出量。
价格(元) 此价格累计卖出量(公斤)
6.35 120
6.37 300
6.36 50
6.40 70
希望大家帮忙,成功了另有高分相送!!
------解决思路----------------------
非要用对象数组吗,Map更简单。
------解决思路----------------------
Map<Double, Integer> map=new HashMap<Double, Integer> ();
public void insertMapData(Double key,Integer value) {
map.put(key, (map.get(value)==null)?value:value+map.get(value));
}
------解决思路----------------------
map.put(key, (map.get(key)==null)?value:value+map.get(value));
------解决思路----------------------
用固定长度的数组还没有Map 两句话来得干净利落
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; } }
------解决思路----------------------
用固定长度的数组还没有Map 两句话来得干净利落
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;
}
}