对象数组把相同元素的数据汇总后生成一个不重复的没有相同元素

晋儿 2015-07-25 10:30:25
如下,一段时期内的商品价格于相应卖出量的对象数组
日期 当日价格(元) 卖出量(公斤)
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


希望大家帮忙,成功了另有高分相送!!
...全文
174 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
飏飏一蝶 2015-07-26
  • 打赏
  • 举报
回复
用固定长度的数组还没有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;
	}
	 
	 
 }
飏飏一蝶 2015-07-26
  • 打赏
  • 举报
回复
用固定长度的数组还没有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; 	} 	 	 } 
晋儿 2015-07-25
  • 打赏
  • 举报
回复
或者给小弟一个完整的代码好吗,谢谢
晋儿 2015-07-25
  • 打赏
  • 举报
回复
前辈能用对象数组解决吗,多谢
shiyi_xie 2015-07-25
  • 打赏
  • 举报
回复
引用 3 楼 shiyi_xie 的回复:
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));
shiyi_xie 2015-07-25
  • 打赏
  • 举报
回复
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)); }
晋儿 2015-07-25
  • 打赏
  • 举报
回复
最好用对象数组的方法, 这里有个去除重复加总的代码,请帮忙完善一下 public boolean equals(Object obj) { // TODO Auto-generated method stub DateS ds = (DateS)obj; if(this.price==ds.price){ ds.mount = this.mount+ds.mount; return true; }else{ return false; } } 关键代码,用两个ArrayList
飏飏一蝶 2015-07-25
  • 打赏
  • 举报
回复
非要用对象数组吗,Map更简单。

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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