简体   繁体   中英

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:

three   three
two     XXX
ten     ten
XXX     two
five    five

I've added XXX to view them easier. You should also check the case when the lists are not equal in size since it might change the expected output of your program.

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. 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

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:

Putting empty in list2
Putting empty in list1

three three
two 
ten   ten
      two
five  five

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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