java数据求和

caesh 2008-02-21 03:19:41
一组数据,例如:
time finishJobs
2004 1
2005 1
2005 1
2005 1
2006 1
2007 1
需要java程序将time一样的数据进行求和,例如:
time finishJobs
2004 1
2005 3
2006 1
2007 1

请达人赐教!!!!
...全文
454 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycyn521 2008-02-22
  • 打赏
  • 举报
回复
这个方法不是最好的,但是算法不错,已通过测试!

//已通过测试
public class Test{


//打印方法
public static void print(int[][] a){
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++ ){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
//主算法,新建一个数组,查找重复的同时,把1加上,然后赋值给新的数组
public static int[][] done(int[][] a){

int[][] b = {{0,0},{0,0},{0,0},{0,0}};
int m = 0;
//数组内部比较开始
for(int i = 0; i < a.length; i++){
int sum = a[i][1];

if(a[i][0] != 0){
for(int j = i+1; j < a.length; j++){
if (a[i][0] == a[j][0]){
sum += a[j][1];
a[j][0] = 0;
}

}
//向新数组赋值
b[m][0] = a[i][0];
b[m][1] = sum;
m++;
}
}
//返回一个新数组
return b;
}
public static void main( String[] args ){
int[][] a = {{2004,1},{2005,1},{2005,1},{2005,1},{2006,1},{2007,1}};
print(a);
System.out.println("----------计算后的结果--------");
print(done(a));

}
}




输出结果为:
2004 1
2005 1
2005 1
2005 1
2006 1
2007 1
----------计算后的结果--------
2004 1
2005 3
2006 1
2007 1
caesh 2008-02-22
  • 打赏
  • 举报
回复
谢谢大家了 分值平分 多谢多谢
xiaoyu_air 2008-02-21
  • 打赏
  • 举报
回复
放数据库里多好啊.用关键字
distinct
ycyn521 2008-02-21
  • 打赏
  • 举报
回复
...
HellMoxi 2008-02-21
  • 打赏
  • 举报
回复
键值对,用键验证,相同就把对应的值累加上!!!
rain_night 2008-02-21
  • 打赏
  • 举报
回复
LZ的 那要求只能实现其中一组key重复的,若有2组重复的就出错,还是楼主自己想办法吧,我的代码态复杂了,仅提供参考


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class MapUsing {
public static void main(String[] args) {
Date[] d = new Date[9];
d[0] = new Date(2004, 1);
d[1] = new Date(2005, 1);
d[2] = new Date(2005, 1);
d[3] = new Date(2005, 1);
d[4] = new Date(2005, 1);
d[5] = new Date(2005, 1);
d[6] = new Date(2006, 1);
d[7] = new Date(2008, 1);
d[8] = new Date(2009, 1);
Arrays.sort(d);
HashMap<Integer,Integer> value=exChange(d);
System.out.println("2004 "+value.get(2004));
System.out.println("2005 "+value.get(2005));
System.out.println("2006 "+value.get(2006));
System.out.println("2008 "+value.get(2008));
System.out.println("2009 "+value.get(2009));
}

public static HashMap<Integer, Integer> exChange(Date[] input) {
int count = 0;
int i = 0;
int j = i + 1;
boolean flag = false;
HashMap<Integer, Integer> result = new HashMap<Integer, Integer>(10);
while (i < input.length && j < input.length) {
if (flag) {
result.put(input[i].year, (count + 1));
} else {
result.put(input[i].year, input[i].count);
}
if (input[i].equals(input[j])) {
count++;
j++;
i = j - 1;
flag = true;
} else {
i++;
j = i + 1;
flag = false;
}
if (flag) {
result.put(input[i].year, (count + 1));
} else {
result.put(input[i].year, input[i].count);
}

}
return result;
}
}

class Date implements Comparable {
public int compareTo(Object o) {
if (this.year > ((Date) o).year) {
return 1;
} else if (this.year < ((Date) o).year) {
return -1;
}
return 0;
}

public int year, count;

public Date(int year, int count) {
this.year = year;
this.count = count;
}

public String toString() {
return this.year + " " + this.count;
}

public boolean equals(Object o) {
if (this.year == ((Date) o).year) {
return true;
}
return false;
}
}

yibunengjing 2008-02-21
  • 打赏
  • 举报
回复
String[] year = new String[] { "2004", "2005", "2005", "2005", "2006",
"2007" };
int[] value = { 1, 1, 1, 1, 1, 1 };
Map map = new HashMap();
for (int i = 0; i < year.length; i++) {
String thisYear = year[i];
int count = value[i];
if (map.containsKey(thisYear)) {
count = Integer.parseInt(map.get(thisYear).toString())
+ value[i];
}
map.put(thisYear, count);
}
yibunengjing 2008-02-21
  • 打赏
  • 举报
回复
String[] year = new String[]{"2004", "2005", "2005", "2005", "2006", "2007"};
int[] value = {1, 1, 1, 1, 1, 1};
Map map = new HashMap();
for (int i = 0; i < year.length; i++) {
String thisYear = year[i];
int count = value[i];
if (map.containsKey(thisYear)) {
count = Integer.parseInt(map.get(thisYear).toString()) + value[i];
}
map.put(thisYear, count);
}
wuxo84 2008-02-21
  • 打赏
  • 举报
回复
可用性不是很好啊

public class Test
{
public static void main( String[] args )
{
int[] time = new int[5];
time[0] = 2004;
time[1] = 2005;
time[2] = 2005;
time[3] = 2006;
time[4] = 2007;
int count = 0;

for( int i=2004; i<2008; i++ )
{
for( int j=0; j<time.length; j++ )
{
if( time[j] == i )
count += 1;
}
System.out.println( i + ": " + count );
count = 0;
}
}
}
封师傅 2008-02-21
  • 打赏
  • 举报
回复
大致思路:

Map map=new HashMap();
如果 map.get(time)是空,增加map.put(time,finishJobs )
否则,map.put(time,map.get(time)+finishJobs )
JadoNet 2008-02-21
  • 打赏
  • 举报
回复
用map不行吗?key="200*" value="自己加"

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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