Java 比较两个大小相同的字符串列表,其中顺序很重要

Java compare two lists of String of same size where order is important

我目前在比较两个字符串列表时卡住了。 以下是输入:

First list : three, two, ten, five.
Second list: three, ten, two, five.

顺序在两个列表中都很重要:如果一个元素索引在另一个列表中不相同,那么它应该放一个空行。

为了更清楚起见,我附上了屏幕截图。

这是我的代码

public static void main(String[] args) {

 List<String> list1 = new ArrayList<String>();
 List<String> list2 = new ArrayList<String>();


    list1.add("three");
    list1.add("two");
    list1.add("ten");
    list1.add("five");

    list2.add("three");
    list2.add("ten");
    list2.add("two");
    list2.add("five");

    for(int iIndex = 0, jIndex = 0; iIndex < list1.size() && jIndex < list2.size(); iIndex ++, jIndex++) {
        if(!list1.get(iIndex).contentEquals(list2.get(jIndex))) {
            list1.add(jIndex, "");
        }
    }

注意:在发布这个问题之前,我已经搜索并检查了每个列出的主题。 感谢您的帮助

如果我正确理解你的问题,你需要这样的东西

int size = list2.size();
for(int i = 0; i < size ; i++) {
    if(!list1.get(i).contentEquals(list2.get(i))) {
      if(list2.size() <= list1.size()) {
        list2.add(i, "XXX");
        size +=1;
      } else {
        list1.add(i, "XXX");
      } 
    }
}

输出将是:

three   three
two     XXX
ten     ten
XXX     two
five    five

我添加了 XXX 以便更轻松地查看它们。 您还应该检查列表大小不相等的情况,因为它可能会改变程序的预期输出。

更新:

你可以尝试这样做。

int size = list1.size();
for(int i = 0; i < size; i++) {
  if(list1.size() == i){
    list1.add(i, "XXX");
    continue;
  }
  if(list2.size() == i){
    list2.add(i, "XXX");
    continue;
  }
  if(!list1.get(i).contentEquals(list2.get(i))) {
    int next_index1 = list2.subList(i, list2.size()).indexOf(list1.get(i));
    int next_index2 = list1.subList(i, list1.size()).indexOf(list2.get(i));

    if (next_index1 == -1){
      list2.add(i, "XXX");
    }
    else if(next_index2 == -1){
      list1.add(i, "XXX");
    } 
    else if(next_index1 < next_index2) {
      list1.add(i, "XXX");
    } else {
      list2.add(i, "XXX");
    } 
  }
  size = list1.size() < list2.size() ? list2.size() : list1.size();
}
for(int i = 0; i < size ; i++) {
  System.out.println(list1.get(i) + "   " + list2.get(i));
}

简而言之,它将检查字符串何时最接近另一个列表中的出现。 例如,如果您的列表是:

three   three
two     ten
ten     two
five    five

在每个列表中的第一个元素之后,因为它们是相同的,它会找到第一个列表中的当前位置与第二个列表中该元素第一次出现的索引之间的距离,反之亦然大约。如果距离相等,那么它将在第二个列表中添加 space。 所以结果将是

three   three
two     XXX
ten     ten
XXX     two
five    five

但是如果你的列表是

three   three
two     ten
five     two

输出将是

three   three
XXX     ten
two     two
five    XXX

问题,正如我所看到的那样,无法确定哪个列表应该插入一个空行。所以我会在两者中插入一个。这假定两个列表的开始长度相等。在循环中操作列表的大小时,我更喜欢反向操作以确保任何索引都保持同步。

for (int i = list1.size()-1; i >= 0; i--) {
    if (list1.get(i) != list2.get(i)) {
        list1.add(i,"");
        list2.add(i+1,"");
    }
}

for (int i = 0; i < list1.size(); i++) {
    System.out.printf("%7s  %7s%n",list1.get(i),list2.get(i));
}

版画

  three    three
             ten
    two         
             two
    ten         
   five     five

也可能是这样。

 three    three
    two         
             ten
    ten         
             two
   five     five

根据屏幕截图,我可以看到如果元素不相等,则必须将这些元素与列表中的下一个元素进行比较。但是,仍然必须做出一个假设。也就是如果相同则选择哪个元素的假设。

我的假设是,如果列表的长度相等,那么将选择第一个列表的值。如果没有这个假设,我认为这个问题没有解决方案。

//Assuming list1.size() == list2.size() from the start
int listSize = list1.size();

for(int i=0; i<listSize; i++){
    // If both are equal
    if(list1.get(i).equals(list2.get(i))){
        continue;
    }
    // If lists have the same size, pick value of list1
    if(list1.size() == list2.size()){
        System.out.println("Putting empty in list2");
        list2.add(i,"");
        listSize+=1;
    }
    else{
        System.out.println("Putting empty in list1");
        list1.add(i,"");
    }
}

输出:

Putting empty in list2
Putting empty in list1

three three
two 
ten   ten
      two
five  five