我是设计的新手,我想在彼此后面添加2个帖子,以进行如下设计:
然后是标题
发布1,然后发布2
发布3,发布4然后
帖子5,帖子6然后
页脚
当我尝试使它出现问题时,发布1,发布1然后发布2,发布2
public class TestClass {
private int value;
private int lowestValue;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getLowestValue() {
return lowestValue;
}
public void setLowestValue(int lowestValue) {
this.lowestValue = lowestValue;
}
public static void main(String args[]) {
TestClass tc = new TestClass();
ArrayList<TestClass> al = new ArrayList<TestClass>();
int temp = 0;
// create list (example)
tc.setValue(3);
al.add(tc);
tc = new TestClass();
tc.setValue(2);
al.add(tc);
tc = new TestClass();
tc.setValue(1);
al.add(tc);
tc = new TestClass();
tc.setValue(3);
al.add(tc);
tc = new TestClass();
tc.setValue(2);
al.add(tc);
tc = new TestClass();
tc.setValue(3);
al.add(tc);
// process
for (int i = 0; i < al.size(); i++) {
// assign value to temp (to compare later on)
if (i == 0) {
temp = al.get(i).getValue();
}
if (i > 0 ) {
// if temp value is lower than current element value, reverse loop to update the lowest value variable in list
if (temp < al.get(i).getValue()) {
for (int j = i; j >= 0 && j <= i; j--) {
System.out.println("i: " + i + ", j: " + j);
if (al.get(j).getLowestValue() != 0) {
al.get(j).setLowestValue(temp);
}
}
temp = al.get(i).getValue();
} else if (temp >= al.get(i).getValue()) { // unfinished part
temp = al.get(i).getValue();
}
}
}
}
}