-1

This is a noob question, sorry, but I am totally confused.

I had created a class called CalendarItems.

The fields are:

int index
String description
String textdate
int daysaway

The constructor uses the first three fields. And for the moment I have created four objects within the class.

I now want to do two things:

  1. Loop through the CalendarItem's For each CalendarItem calculate daysaway, which is the difference between the text date and today. I have figured out how to calculate this and can manually add do it by calling my method for item1, then for item2, etc

But i'd like to understand how i can do this with a loop. If i was in javascript i'd have an multi-dimensional array and could easy create a loop to cycle through it, but in the scary new world of java I am lost

  1. Sort my CalendarItems on the daysaway field I want to do this so that I can display them as CardViews in order of soonest-first.

[Inflating CardViews has proved blissfully straight-forward, and I can (again, with a manual 'loop') inflate a card for each CalendarItem and add the various fields to it. And creating the class and fields and adding my 4 sample objects has also worked, so it hasn't been a total lost weekend.]

I think where I am going wrong is that I keep thinking in terms of multi-dimensional arrays from my experience with javascript and Excel VBA, and however much I read about objects and iterator interfaces and arraylists in java, my brain is just refusing to take it in.

I have Google'd a lot on this, but every 'simple guide to iterators' has left me more confused than when I started.

Thanks in advance to anyone patient enough to help me.

Willian Soares
  • 320
  • 4
  • 16
CFO
  • 288
  • 1
  • 11
  • 3
    Instead of describing your code, could you show what you currently have and some sort of demonstration of what you are wanting for your result? – Zannith Jun 04 '18 at 19:37

3 Answers3

1

For (1), what have you tried? If you need a multi-dimensional array, nothing stops you from using one. Consider:

int dim1 = 5;
int dim2 = 10; // whatever you need
CalendarItem[][] items = new CalendarItem[dim1][dim2];

Im this case you would iterate your array the same way as with JavaScript.

for (int i = 0; i < dim1; i++) {
     for (int j = 0; j < dim2; j++) {
         // do whatever you need to do
     }
}

You can also read about the enhanced for loop if you don't actually need a numerical index.

For (2) have a look at the Comparator interface in the javadocs as well as the methods in the Collections class, in particolar the sort method.

geco17
  • 5,152
  • 3
  • 21
  • 38
1
For 1) you can either create array in java or you can use ArrayList and store your objects in that arraylist and iterate over arraylist.

ArrayList<CalendarItem> list = new ArrayList();
list.add(obj1); // obj1 is the instance you created for the calendar item.

for(CalendarItem c: list){
// this is loop , do whatever you want here
}

For sorting, Either write a comparator class or implement comparable interface to your CalenderItem and override method compareTo. In this method you can provide comparison logic based on requirede field.
  • Many thanks for this. I now have part one successfully working and am adding CardViews for each CalendarItem. – CFO Jun 05 '18 at 06:19
1

I post my solution, as I already wrote it, it should be a complete one:

public class CalendarItem implements Comparable<CalendarItem> {
    private int index;
    private String description;
    private ZonedDateTime textdate;
    private long daysAway = 0;

    public CalendarItem(int index, String description, ZonedDateTime textdate) {
        this.index = index;
        this.description = description;
        this.textdate = textdate;
        this.calculateDaysAway();
    }

    private void calculateDaysAway() {
        this.daysAway = ChronoUnit.DAYS.between(textdate, ZonedDateTime.now(ZoneId.systemDefault()));
    }

    public long getDaysAway() {
        return daysAway;
    }

    @Override
    public int compareTo(CalendarItem o) {
        if (this.daysAway < o.daysAway) {
            return -1;
        } else if (this.daysAway > o.daysAway) {
            return 1;
        } else {
            return 0;
        }
    }

    public static void main(String[] args) {
        CalendarItem c1 = 
                new CalendarItem(0, "One", ZonedDateTime.of(2018, 5, 21, 0, 0, 0, 0, ZoneId.systemDefault()));
        CalendarItem c2 = 
                new CalendarItem(0, "Two", ZonedDateTime.of(2018, 5, 4, 0, 0, 0, 0, ZoneId.systemDefault()));
        CalendarItem c3 = 
                new CalendarItem(0, "Three",ZonedDateTime.of(2018, 5, 11, 0, 0, 0, 0, ZoneId.systemDefault()));
        CalendarItem c4 = 
                new CalendarItem(0, "Four", ZonedDateTime.of(2018, 5, 1, 0, 0, 0, 0, ZoneId.systemDefault()));

        List<CalendarItem> calendarItems = new ArrayList<>(Arrays.asList(c1, c2, c3, c4));

        Collections.sort(calendarItems);

        for (CalendarItem item : calendarItems) {
            System.out.println("Item " + item.getDescription() + " is " + item.getDaysAway() + " days away.");
        }
    }
}

Note: I am using ZonedDateTime from java 8 to represent the date.

NiVeR
  • 9,644
  • 4
  • 30
  • 35