简体   繁体   English

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

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

I am currently stuck while comparing two list of strings.我目前在比较两个字符串列表时陷入困境。 Here are the inputs:以下是输入:

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

The order is important in both list in such a way that: if one element index is not same in other list, then it should put an empty line.顺序在两个列表中都很重要,因为:如果一个元素索引在另一个列表中不同,那么它应该放置一个空行。

I have attached a screen shot for better clarity.为了更清晰,我附上了屏幕截图。

在此处输入图像描述

Here is my code这是我的代码

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

Note : I have searched and checked each listed topics before posting this question.注意:在发布此问题之前,我已经搜索并检查了每个列出的主题。 Thank you for your help谢谢您的帮助

If I understood your question correctly you need something like this如果我正确理解你的问题,你需要这样的东西

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

And the output will be: output 将是:

three   three
two     XXX
ten     ten
XXX     two
five    five

I've added XXX to view them easier.我添加了 XXX 以便更轻松地查看它们。 You should also check the case when the lists are not equal in size since it might change the expected output of your program.您还应该检查列表大小不相等的情况,因为它可能会更改程序的预期 output。

UPDATE:更新:

You can try to do something like this.你可以尝试做这样的事情。

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

In brief, it will check when is the closest occurrence of a string in the other list.简而言之,它将检查何时是另一个列表中最接近的字符串。 For example if your lists are:例如,如果您的列表是:

three   three
two     ten
ten     two
five    five

After the first elements in each list, because they are the same, it will find the distance between the current position in the first list and the index of the first occurrence of that element in the second list and the other way around.在每个列表中的第一个元素之后,因为它们相同,它会找到第一个列表中当前 position 与第二个列表中该元素第一次出现的索引之间的距离,反之亦然。 If the distances are equal then it will add space in the second list.如果距离相等,那么它将在第二个列表中添加空间。 So the result will be所以结果将是

three   three
two     XXX
ten     ten
XXX     two
five    five

But if your list are但如果你的清单是

three   three
two     ten
five     two

the output will be output 将是

three   three
XXX     ten
two     two
five    XXX

The problem, as I see it is that there is no way to determine which list should be inserted with a blank line.正如我所看到的,问题在于无法确定应该在哪个列表中插入空白行。 So I would insert one in both.所以我会在两者中插入一个。 This presumes both lists are equal in length to start.这假定两个列表的开始长度相等。 When manipulating the size of a list within a loop I prefer to do it in reverse to ensure that any indices will remain in sync.在循环中操作列表的大小时,我更喜欢反向操作,以确保所有索引都保持同步。

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

Prints印刷

  three    three
             ten
    two         
             two
    ten         
   five     five

or it could have been like this.或者它可能是这样的。

 three    three
    two         
             ten
    ten         
             two
   five     five

As per the screenshot I can see that if the elements are not equal, then the elements have to be compared with the next element in the list.根据屏幕截图,我可以看到如果元素不相等,则必须将元素与列表中的下一个元素进行比较。 However, there is still one assumption that has to be made.但是,仍然需要做出一个假设。 That is the assumption of which element to choose if they are the same.这是假设如果它们相同,则选择哪个元素。

My assumption is that if the lists are equal in length, then the value of the first list will be chosen.我的假设是,如果列表的长度相等,那么将选择第一个列表的值。 Without this assumption, I don't think there is a solution to this problem.如果没有这个假设,我认为这个问题没有解决方案。

//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: 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