【发布时间】:2016-06-29 14:58:27
【问题描述】:
我有一个一周内完成任务的人的开始和停止时间列表。
当他们更改任务时,会创建一个新条目。
此信息存储在二维数组中。 - 我想遍历这个数组并将数据存储在另一个数组中。
在新的 2D 数组中,我想要一行来显示特定日期的人。
所以如果第一个数组存储:(Person,Day,In,Out,Total)
{{"John","Mon","08:00","12:00","4.00"},
{"John","Mon","12:00","17:00","5.00"},
{"John","Tue","08:00","17:00","9.00"},
{"Mike","Tue","08:00","11:00","3.00"}
{"Mike","Tue","11:00","17:00","6.00"}};
我想要存储第二个数组:
{{"John","Mon","08:00","17:00","9.00"},
{"John","Tue","08:00","17:00","9.00"},
{"Mike","Tue","08:00","17:00","9.00"}};
到目前为止,这是我的代码:
public class CompArrayTest {
public static void main(String args[]){
String[][] End = new String [5][5];
String[][] Start = {{"John","Mon","08:00","12:00","4.00"},
{"John","Mon","12:00","17:00","5.00"},
{"John","Tue","08:00","17:00","9.00"},
{"Mike","Tue","08:00","11:00","3.00"},
{"Mike","Tue","11:00","17:00","6.00"}};
//print start
for(int i = 0; i<Start.length; i++){
for(int j = 0; j<Start.length; j++){
System.out.print(Start[i][j]+" ");
}//j end
System.out.print("\n");
}//i end
//change end
for(int i = 0; i<Start.length; i++){
String name = Start[i][0];
String day = Start[i][1];
String In = Start[i][2];
String Out = Start[i][3];
String Total = Start[i][4];
//look through End
for(int j = 0; j<5; j++){
String eN= End[j][0];
String eD= End[j][1];
if(eN==name && eD==day){
//change end time
End[j][3]=Start[i][3];
//parse and add times
double TS = Double.parseDouble(Start[i][4]);
double TE = Double.parseDouble(End[i][4]);
double ans = TS + TE;
String ANS = ans+"";
End[j][4]= ANS;
} else {
End[j][0] = name;
End[j][1] = day;
End[j][2] = In;
End[j][3] = Out;
End[j][4] = Total;
}//else end
}//j end
System.out.print("\n");
}//i end
//print end
for(int i = 0; i<Start.length; i++){
for(int j = 0; j<Start.length; j++){
System.out.print(End[i][j]+" ");
}//j end
System.out.print("\n");
}//i end
}//main end
【问题讨论】:
-
比方说,一种更简单的方法可能是为“员工”创建一个类。使其成员变量为姓名、进出时间和工作时间
标签: java arrays multidimensional-array