简体   繁体   English

如何交换java中的两个日期

[英]How to swap two dates in java

This is my code.这是我的代码。 What am I missing in it?我错过了什么?

public class Date {

    public static void main(String[] args) {
        Date dt[] = new Date[2];
          dt[0] = new Date(1,2,3);
          dt[1] = new Date(4,5,6);

        System.out.println("Before: "+dt[0]+" "+dt[1]);
        swap(dt[0],dt[1]);
        System.out.println("After: "+dt[0]+" "+dt[1]);
    }
    public void swap(Date dArr[]){
        Date temp ;
        temp = dArr[0];
        dArr[0]=dArr[1];
        dArr[1]=temp;
    }
}


input: d1 = 1/2/3 d2 = 4/5/6 output: d1 = 4/5/6 d2 = 1/2/3输入:d1 = 1/2/3 d2 = 4/5/6 output:d1 = 4/5/6 d2 = 1/2/3

public void swap(Date dArr[]){
    Date temp ;
    temp = dArr[0];
    dArr[0]=dArr[1];
    dArr[1]=temp;
}

Your swap method takes []Date as an argument, and swap the first and second element of the given array.您的swap方法将[]Date作为参数,并交换给定数组的第一个和第二个元素。

Now, you call swap method like this:现在,您可以像这样调用swap方法:

swap(dt[0],dt[1]);

You actually passed two Date object.您实际上传递了两个Date object。 The swap method creates an implicit []Date object as the input argument whose first element is dt[0] and second dt[1] . swap方法创建一个隐式[]Date object 作为输入参数,其第一个元素是dt[0]和第二个dt[1] Note that this array of Date is not the same as your Date dt[] , they are independent of each other.请注意,此 Date 数组与您的Date dt[]不同,它们彼此独立。 You can think of Date dArr[] as containing two pointers to the Date objects in the old Date dt[] .您可以将Date dArr[]视为包含两个指向Date dt[]中的Date对象的指针。 When you call dArr[0]=dArr[1];当你调用dArr[0]=dArr[1]; , you just make the pointer point to another Date object, you can't affect the elements in Date dt[] . ,您只需将指针指向另一个 Date object,您不能影响Date dt[]中的元素。

If you want to change the original Date dt[] , you should pass dt itself as argument:如果要更改原始Date dt[] ,则应将dt本身作为参数传递:

swap(dt)
package le9_ps1;
//import java.util.Date;

/* **********************************************************************  *
 *                           Exercise-9                                    *
 *   Objective: Pass parameters to methods - by value and by  reference.   *
 *                                                                         *
 *   Pre-condition: Date class should be created.                          *
 *   Problem Statement-1: Use the Date class created in Lab Ex1 to Swap two*
 *      Dates(Hint:use call by value method).                              *
 *  *********************************************************************  */


public class Date {
    int d,m,y;
    public Date(int dd, int mm, int yy) {
        this.d= dd;
        this.m = mm;
        this.y = yy;
    }



    public static void main(String[] args) {
        Date dt[] = new Date[2];
                dt[0] = new Date(1,2,3);
                dt[1] = new Date(4,5,6);

                System.out.println("Before: "+dt[0].d+"/"+dt[0].m+"/"+dt[0].y+ "  "+dt[1].d+"/"+dt[1].m+"/"+dt[1].y);
                dt=swap(dt);
                System.out.println("After: "+dt[0].d+"/"+dt[0].m+"/"+dt[0].y+ "  "+dt[1].d+"/"+dt[1].m+"/"+dt[1].y);
    }
    public static Date[] swap (Date dArr[]){
        Date temp ;
        temp = dArr[0];
        dArr[0]   =  dArr[1];
        dArr[1]  =   temp;
        return dArr;
    }
}

output:

"C:\Program Files\Java\jdk-15.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\lib\idea_rt.jar=1054:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Tukaram\Desktop\Exercise\ch3_le2\out\production\chapter4 le9_ps1.Date
Before: 1/2/3  4/5/6
After: 4/5/6  1/2/3

Process finished with exit code 0
public class Date {

    public static void main(String[] args) {
        Date dt[] = new Date[2];
          dt[0] = new Date(1,2,3);
          dt[1] = new Date(4,5,6);

        System.out.println("Before: "+dt[0]+" "+dt[1]);
        dt = swap(dt);
        System.out.println("After: "+dt[0]+" "+dt[1]);
    }
    public static Date[] swap(Date dArr[]){
        Date temp ;
        temp = dArr[0];
        dArr[0]=dArr[1];
        dArr[1]=temp;
        return dArr;
    }
}

Why don't you simply use Collections to reverse the values?为什么不简单地使用 Collections 来反转值?

List<Date> dates = Arrays.asList(new Date(1, 2, 3), new Date(4, 5, 6));
Collections.reverse(dates); // swaps the dates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM