下面的代码返回4个值(91891,31888,7820,181655),我想将它们存储在4个单独的长值中,以便我以后可以比较它们。
这可能吗?谢谢你的帮助。
private long getLatest(final ForumPage page) {
long latest = -1;
long val1; //91891
long val2; //31881
long val3; //7820
long val4; //181655
for (final Topic t : page.getTopics()) {
Date d;
try {
d = Util.parseDate(t.getTime());
} catch (final ParseException e) {
d = null;
}
if (d != null) {
final long time = d.getTime();
if (time > latest)
latest = time;
}
}
Log.v("COUNT: ", String.valueOf(page.getTopicCount()));
return latest;
}
01-01 11:55:53.928: E/COUNT:(2871): 91891
01-01 11:55:54.548: E/COUNT:(2871): 31881
01-01 11:55:54.796: E/COUNT:(2871): 7820
01-01 11:55:56.378: E/COUNT:(2871): 181655
使用getTopicCount编辑,ForumPage类:
public class ForumPage implements Serializable {
private static final long serialVersionUID = 1057995133618054945L;
private int page;
private int maxPage;
private int nextPageStart;
private int topicCount;
private String name;
private final List<Topic> topics = new ArrayList<Topic>();
public int getPage() {
return page;
}
public void setPage(final int page) {
this.page = page;
}
public int getMaxPage() {
return maxPage;
}
public void setMaxPage(final int maxPage) {
this.maxPage = maxPage;
}
public int getNextPageStart() {
return nextPageStart;
}
public void setNextPageStart(final int nextPageStart) {
this.nextPageStart = nextPageStart;
}
public int getTopicCount() {
return topicCount;
}
public void setTopicCount(final int topicCount) {
this.topicCount = topicCount;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public void addTopic(final Topic topic) {
topics.add(topic);
}
public Topic[] getTopics() {
return topics.toArray(new Topic[topics.size()]);
}
}
答案 0 :(得分:1)
在您的班级中创建一个Integer(或ArrayList,如您所愿)的列表:
List<Integer> valuesList = new List<Integer>();
然后保存您的值:
private long getLatest(final ForumPage page) {
long latest = -1;
for (final Topic t : page.getTopics()) {
Date d;
try {
d = Util.parseDate(t.getTime());
} catch (final ParseException e) {
d = null;
}
if (d != null) {
final long time = d.getTime();
if (time > latest)
latest = time;
}
}
Log.v("COUNT: ", String.valueOf(page.getTopicCount()));
valuesList.add(page.getTopicCount());
return latest;
}
您可以在valuesList
这里编辑的是getTopics和getLatest函数:
public Topic[] getTopics() { //4 Topics
return topics.toArray(new Topic[topics.size()]);
}
public long getLatest(final String suffix) {
final String latest = get(KEY_LATEST + suffix);
if (latest != null) {
try {
return Long.parseLong(latest);
} catch (final NumberFormatException e) {
//
}
}
return -1;
}