Java字符串数组合并排序

Java字符串数组合并排序,java,arrays,string,ilmerge,Java,Arrays,String,Ilmerge,嗨,我为一个字符串数组编写了一个mergesort程序,它从用户那里读取.txt文件。但我现在想做的是比较两个文件,打印出文件1中的单词,而不是文件2中的单词,例如苹果在文件1中,而不是文件2。我尝试再次将其存储在字符串数组中,然后在最后打印出来,但我似乎无法实现它。 这是我的 FileIO reader = new FileIO(); String words[] = reader.load("C:\\list1.txt"); String list[] = reader.

嗨,我为一个字符串数组编写了一个mergesort程序,它从用户那里读取.txt文件。但我现在想做的是比较两个文件,打印出文件1中的单词,而不是文件2中的单词,例如苹果在文件1中,而不是文件2。我尝试再次将其存储在字符串数组中,然后在最后打印出来,但我似乎无法实现它。 这是我的

FileIO reader = new FileIO();
     String words[] = reader.load("C:\\list1.txt");
     String list[] = reader.load("C:\\list2.txt");

     mergeSort(words);
     mergeSort(list);
     String x = null ;

     for(int i = 0; i<words.length; i++)
     {
         for(int j = 0; j<list.length; j++)
         {
                 if(!words[i].equals(list[j]))
                 {
                      x = words[i];
                 }
         }
     }

     System.out.println(x);
FileIO reader=newfileio();
字符串字[]=reader.load(“C:\\list1.txt”);
字符串列表[]=reader.load(“C:\\list2.txt”);
合并排序(字);
合并排序(列表);
字符串x=null;

对于(int i=0;i如果要检查第一个数组中但第二个数组中不存在的单词,可以执行以下操作:

 boolean notEqual = true;           
 for(int i = 0; i<words.length; i++)
     {
         for(int j = 0; j<list.length && notEqual; j++)
         {
                 if(words[i].equals(list[j]))     // If the word of file one exist
                 {                                // file two we set notEqual to false
                      notEqual = false;           // and we terminate the inner cycle
                 }
         }
         if(notEqual)                      // If the notEqual remained  true
           System.out.println(words[i]);   // we print the the element of file one
                                           // that do not exist in the second file

         notEqual = true;                  // set variable to true to be used check
     }                                     // the other words of file one.
 for(int i = 0; i<words.length; i++)
 {
     boolean wordFoundInList = false;

     for(int j = 0; j<list.length; j++)
     {
             if(words[i].equals(list[j]))
             {
                  wordFoundInList = true;
                  break;
             }
     }

     if (!wordFoundInList) {
         System.out.println(x);
     }
 }

使用方法将数组转换为列表,并使用方法contains验证第一个文件中的单词是否在第二个文件中。

这看起来有点接近。您所做的是对
单词中的每个字符串进行比较,将其与
列表中的每个单词进行比较,因此如果
列表中有一个字符串
这不在
单词中,
x
正在设置

我建议将
if(!words[I].equals(list[j])更改为
if(words[I].equals(list[j])
。现在您知道
words
中的字符串出现在
list
中,因此您不需要显示它。如果您在
list
中完全循环,而没有看到该单词,则您知道需要对其进行解释。例如:

 boolean notEqual = true;           
 for(int i = 0; i<words.length; i++)
     {
         for(int j = 0; j<list.length && notEqual; j++)
         {
                 if(words[i].equals(list[j]))     // If the word of file one exist
                 {                                // file two we set notEqual to false
                      notEqual = false;           // and we terminate the inner cycle
                 }
         }
         if(notEqual)                      // If the notEqual remained  true
           System.out.println(words[i]);   // we print the the element of file one
                                           // that do not exist in the second file

         notEqual = true;                  // set variable to true to be used check
     }                                     // the other words of file one.
 for(int i = 0; i<words.length; i++)
 {
     boolean wordFoundInList = false;

     for(int j = 0; j<list.length; j++)
     {
             if(words[i].equals(list[j]))
             {
                  wordFoundInList = true;
                  break;
             }
     }

     if (!wordFoundInList) {
         System.out.println(x);
     }
 }

for(inti=0;i您也可以遍历循环,在到达list.length-1时添加它。
如果匹配的话,你可以把所有的东西都打破

FileIO reader = new FileIO();
String words[] = reader.load("C:\\list1.txt");
String list[] = reader.load("C:\\list2.txt");

mergeSort(words);
mergeSort(list);
//never ever null
String x = "" ;

for(int i = 0; i<words.length; i++)
{
    for(int j = 0; j<list.length; j++)
    {
            if(words[i].equals(list[j]))
                 break;
            if(j == list.length-1)
                 x += words[i] + " ";
    }
}

System.out.println(x);
FileIO reader=newfileio();
字符串字[]=reader.load(“C:\\list1.txt”);
字符串列表[]=reader.load(“C:\\list2.txt”);
合并排序(字);
合并排序(列表);
//永不失效
字符串x=“”;
for(inti=0;i这里有一个版本(尽管它不使用排序)


为什么不把数组转换成集合呢?这样你就可以简单地做了 结果=wordsSet.removeAll(列表集)

您的结果将包含list2.txt中不存在的所有单词


还请记住,集合将删除重复项;)

谢谢,这就像我一直在寻找的一样,但我正在尝试尽快将其进行比较。有没有其他方法可以更快地进行比较,或者有没有其他方法可以使其尽快进行比较?据我所知,没有。在一天结束时,你必须循环检查所有内容,直到你检查完所有内容,或者你找到了两个列表中都有一个字符串。在这种情况下,您可以打破循环,这就是我所做的。
Not in file2 [word1, word4]