如何在Java中操作2D数组数据

时间:2016-06-29 14:58:28

标签: java arrays multidimensional-array

我列出了一周内完成任务的个人的开始和停止时间。

当他们更改任务时,会创建一个新条目。

此信息存储在2D数组中。 - 我想遍历这个数组并将数据存储在另一个数组中。

在新的2D数组中,我想要一行来显示特定日期的某个人。

因此,如果第一个数组存储:(人,日,进,出,总)

{{"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

3 个答案:

答案 0 :(得分:0)

1)您可以为Employee创建一个类,但我的答案将处理您选择2D数组的风格。

2)你不应该为完成的数组分配一个5乘5的数组。当然,列数可以是5,但如果您的示例中有重复项,该怎么办?

要解决第二个问题,我会使用 java.lang.UnsatisfiedLinkError: No implementation found for boolean com.esri.core.runtime.LicenseImpl.nativeIsClientIdValid(java.lang.String) (tried Java_com_esri_core_runtime_LicenseImpl_nativeIsClientIdValid and Java_com_esri_core_runtime_LicenseImpl_nativeIsClientIdValid__Ljava_lang_String_2) at com.esri.core.runtime.LicenseImpl.nativeIsClientIdValid(Native Method) at com.esri.core.runtime.LicenseImpl.b(SourceFile:103) at com.esri.android.runtime.ArcGISRuntime$License.b(SourceFile:133) at com.esri.android.runtime.ArcGISRuntime$License.a(SourceFile:72) at com.esri.android.runtime.ArcGISRuntime.setClientId(SourceFile:51) at com.ihs.connect.App.configureArcGIS(App.java:89) at com.ihs.connect.App.onCreate(App.java:82) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1036) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6316) at android.app.ActivityThread.access$1800(ActivityThread.java:221) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 循环和计数器变量来遍历每个人的标识变量。在这种情况下,我将假设每个“名称”都不同,因此您可以检查名称和日期是否相同,如果是,则不要递增计数器。最后,你有一个完美尺寸的2D阵列。

接下来,创建一个算法来计算进出时间的工作小时数。

最后,使用另一个for循环,检查名称和日期是否相同。如果是这样,请添加这些小时数并创建包含该数据的新行。

答案 1 :(得分:0)

我建议你使用一个聚合Session集合的对象Employee。 对象会话:

public class Session {
    private String day;
    private String in;
    private String out;
    private String total;

    Session(String day, String in, String out, String total){
        this.setDay(day);
        this.setIn(in);
        this.setOut(out);
        this.setTotal(total);
}
    public String getDay() {
        return day;
    }
    public void setDay(String day) {
        this.day = day;
    }
    public String getIn() {
        return in;
    }
    public void setIn(String in) {
        this.in = in;
    }
    public String getOut() {
        return out;
    }
    public void setOut(String out) {
        this.out = out;
    }
    public String getTotal() {
        return total;
    }
    public void setTotal(String total) {
        this.total = total;
    }   
}

对象员工:

public class Employee {
    private String name;
    private List<Session> sessions;

    Employee(String name){
        this.name = name;
        this.sessions = new ArrayList<Session>();
    }

    public boolean addSession(String day, String in, String out, String total){
        return sessions.add(new Session(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","12:00","17:00","6.00"}
};

答案 2 :(得分:0)

感谢您的帮助,但我设法找到一个解决方案,在循环启动数组时解决了我的问题。

 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];

        int emptyLine = 0;
        int lastLine = 0;

        //gets first emptyline, lastfilled line
        for (int j = 0; j < 5; j++) {

            if (End[j][0] == null) {
                emptyLine = j;
                if (j > 0) {
                    lastLine = j - 1;

                }
                break;
            }
        }//get Empty

        String eN = End[lastLine][0];
        String eD = End[lastLine][1];

        if (eN == name && eD == day) {
            //change end time
            End[lastLine][3] = Start[i][3];

            //parse and add times
            double TS = Double.parseDouble(Start[i][4]);
            double TE = Double.parseDouble(End[lastLine][4]);

            double ans = TS + TE;
            String ANS = ans + "";

            End[lastLine][4] = ANS;

           // System.out.println("Test If " + name + " " + day + " " + In + " " + Out + " " + Total);

        } else {

            End[emptyLine][0] = name;
            End[emptyLine][1] = day;
            End[emptyLine][2] = In;
            End[emptyLine][3] = Out;
            End[emptyLine][4] = Total;

           // System.out.println("Test Else " + name + " " + day + " " + In + " " + Out + " " + Total);

        }//else end


    }//i end

这给了我想要的输出。但是感谢有关制作员工对象的建议,我将来会使用它。

启动数组

John Mon 08:00 12:00 4.0
John Mon 12:00 17:00 5.0
John Tue 08:00 17:00 9.0
Mike Tue 08:00 11:00 3.0
Mike Tue 11:00 17:00 6.0

结束数组

John Mon 08:00 17:00 9.0
John Tue 08:00 17:00 9.0
Mike Tue 08:00 17:00 9.0
null null null null null null null null null null