我有一个包含 6 个元素的排序数组列表。前 5 个元素有一些值,第 6 个元素为空。
我想循环遍历这个ArrayList,并将第一个记录的前5个元素与下一个记录中的相同元素进行比较。如果任一元素不同,则填充第 6 个元素。
有人知道更简单、更快的方法吗?
谢谢 安加德
最佳答案
首先将所有记录拆分为许多String[]
,然后解析每个记录中的所有值。之后您可以将当前记录与第一个记录进行比较。这是一个例子:
public class StringComparison {
ArrayList<String[]> list = new ArrayList<String[]>();
public void comapreStrings() {
// The list you are comparing everything to
String[] firstRecord = list.get(0);
for(int n = 1; n < list.size(); n++) {
String[] currentRecord = list.get(n);
for (int i = 0; i < currentRecord.length; i++) {
String val1 = firstRecord[i];
String val2 = currentRecord[i];
if (val1.equals(val2)) {
// The two strings are the same
} else {
// Replace "a value" with whatever you want to fill the 6th element with
currentRecord[5] = "a value";
}
}
}
}
关于Java : Compare previous record with current record in arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37229716/