这是个菜鸟问题,对不起,我完全搞不懂了。
我创建了一个名为CalendarItems
的类。
这些字段包括:
int index
String description
String textdate
int daysaway
构造函数使用前三个字段。目前,我已经在类中创建了四个对象。
现在我想做两件事:
循环遍历CalendarItem的For each CalendarItem calculate daysaway,这是文本日期与今天之间的差值。我已经知道了如何计算它,并且可以通过调用我的方法为item1,然后为item2等手动添加do
但我想知道如何使用循环来实现这一点。如果我在javascript中,我会有一个多维数组,并且可以很容易地创建一个循环来遍历它,但是在令人恐惧的java新世界中,我迷失了方向。
CalendarItems
字段中的daysaway
进行排序我要这样做,以便可以按CardViews的顺序将它们显示为膨胀CardViews
已经被证明是非常简单的,我可以(同样,通过一个手动的‘循环’)为每个CalendarItem
膨胀一张卡,并向其中添加各种字段。创建类和字段并添加我的4个样本对象也是有效的,所以这并不是一个完全迷失的周末。
我认为我的错误之处在于,从我使用javascript和Excel VBA的经验来看,我一直在思考多维数组,无论我读了多少关于java中的对象、迭代器接口和数组列表的内容,我的大脑就是拒绝接受它。
我已经在谷歌上做了很多这方面的工作,但是每一个“迭代器的简单指南”都让我比刚开始的时候更加困惑。
提前感谢任何有足够耐心帮助我的人。
发布于 2018-06-05 04:10:57
我发布了我的解决方案,因为我已经写过了,它应该是一个完整的:
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.");
}
}
}
注意:我使用Java8中的ZonedDateTime
来表示日期。
发布于 2018-06-05 03:45:44
对于(1),您尝试过什么?如果你需要一个多维数组,没有什么可以阻止你使用它。考虑一下:
int dim1 = 5;
int dim2 = 10; // whatever you need
CalendarItem[][] items = new CalendarItem[dim1][dim2];
在这种情况下,您可以使用与JavaScript相同的方式迭代数组。
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
// do whatever you need to do
}
}
如果您实际上不需要数字索引,还可以阅读有关增强的for循环的内容。
对于(2),看看javadocs中的比较器接口以及Collections类中的方法,在sort方法中。
发布于 2018-06-05 03:53:35
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.
https://stackoverflow.com/questions/50687626
复制相似问题