繁体   English   中英

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");
      } 
    }
}

output 将是:

three   three
two     XXX
ten     ten
XXX     two
five    five

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

更新:

你可以尝试做这样的事情。

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

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

three   three
two     XXX
ten     ten
XXX     two
five    five

但如果你的清单是

three   three
two     ten
five     two

output 将是

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,"");
    }
}

Output:

Putting empty in list2
Putting empty in list1

three three
two 
ten   ten
      two
five  five

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM