【发布时间】:2018-04-15 18:51:48
【问题描述】:
我正在检查 ArrayList 的先前索引与当前索引具有相同值的运行时间。如果先前的值与当前值的比较值相同,那么我不想再次添加到列表中。我怎样才能做到这一点?请在下面找到我一直在尝试的代码:
List<EventDto> liveEvents = new ArrayList<EventDto>();
EventDto liveTvEventDto = null;
List<CustomerEventDetails> customerDetails = odsRepository.getEventDetails(countryCode, customerId);
if (customerDetails.size() > 0) {
for (CustomerEventDetails customerData : customerDetails) {
liveTvEventDto = new EventDto();
liveTvEventDto.setCountryCode(customerData.getCountryCode());
liveTvEventDto.setCustomerId(customerData.getCustomerId());
ListIterator<EventDto> listIterator = liveEvents.listIterator();
if (listIterator.previousIndex() == -1) {
listIterator.next();
int i =listIterator.previousIndex();
System.out.println("listIterator:"+liveEvents.get(i));
if (!(StringUtility.trimmedOrEmptyIfNull(liveEvents.get(i).getCustomerId()).equals(StringUtility.trimmedOrEmptyIfNull(liveTvEventDto.getCustomerId()))
&& StringUtility.trimmedOrEmptyIfNull(liveEvents.get(i).getCountryCode()).equals(StringUtility.trimmedOrEmptyIfNull(liveTvEventDto.getCountryCode()))))
liveEvents.add(liveTvEventDto);
}
}
}
【问题讨论】:
-
为什么不使用普通循环而不是增强的 for 循环?
-
我怎样才能使用那个循环?
-
我添加了一个答案。看看有没有帮助。
标签: java for-loop arraylist iterator listiterator