-1

I have an array with dates called startDates. I also have an array with dates called endDates. I also have an array with longs called dateDifferences. Now, I print somewhere in my code, the start date, along with the difference (calculated using startDate and endDate) beside it. Then I repeat this for all the Date objects. However, I need to have my startDates printed in order. How can I achieve this so that the difference is beside the respective startDate?

ArrayList <Date> startDates = new ArrayList<Date>();
ArrayList <Date> endDates = new ArrayList<Date>();
ArrayList <Long> dateDifferences = new ArrayList<Long>();

In these arrays I add the pairs startDate and endDate and dateDifference in their respective arraylists.

gettingthere
  • 19
  • 1
  • 6

2 Answers2

0

have u considered making ur three arrays one 2d array with 3 rows:

int[][] multi = new int[3][10]

and sorting it like in this Answer?

    import java.util.Arrays;
    import java.util.Comparator;

public class Asdf {

    public static void main(final String[] args) {
        final String[][] data = new String[][] {
                new String[] { "2009.07.25 20:24", "Message A" },
                new String[] { "2009.07.25 20:17", "Message G" },
                new String[] { "2009.07.25 20:25", "Message B" },
                new String[] { "2009.07.25 20:30", "Message D" },
                new String[] { "2009.07.25 20:01", "Message F" },
                new String[] { "2009.07.25 21:08", "Message E" },
                new String[] { "2009.07.25 19:54", "Message R" } };

        Arrays.sort(data, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);
            }
        });

        for (final String[] s : data) {
            System.out.println(s[0] + " " + s[1]);
        }
    }

}

credit to Bert F. Sort a two dimensional array based on one column

Community
  • 1
  • 1
Kasuyakema
  • 131
  • 1
  • 4
0

It would help if you added more context and/or an example of what you wanted. But, let me try doing that for you. Based on my understanding, an example of what you want is:

start date [1 jan 2016, 4 jan 2016, ...]
end date   [2 jan 2016, 7 jan 2016, ...]
dateDiff   [1, 3, ...]

And then, you want to print something like:

1 jan 2016 : 1 
4 jan 2016 : 3
...

There are several options you have here: 1. You don't need to necessarily need the dateDiff array. Just compute the date difference between start and end dates when you print the start date. And then print the difference right there! 2. If you want to pre-compute it for whatever reason, consider using a better data structure for the job. A map would work if you do not have duplicate start dates, but that might be a bad assumption. And if so, then an array of tuples might work well enough and is usually pretty performant for this type of work.

If this doesn't work for you, tell us why or share more context. You will often find more helpful solutions then!

  • I need to sort the dates in startDate though. If I do sort them then the counterparts in the other arrayLists will be out of order. – gettingthere Apr 05 '16 at 09:03