【问题标题】:comparing two string arrays using java使用java比较两个字符串数组
【发布时间】:2015-04-23 07:28:36
【问题描述】:

我们正在尝试比较两个字符串数组(as[] 和 bs[])并使用 bs[] 中存在的新字符串更新数组字符串 as[]。我们无法更新 as[].Pls帮助我们使用以下代码。谢谢你;)

public class Aa {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {


  // Create an array of 4 strings (indexes 0 - 3)

    String as[] = new String[5];
    String bs[] = new String[16];
    int i;



    try {

        // Create a bufferreader object to read our file with.

       BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
       BufferedReader reader1;
        reader1 = new BufferedReader(new FileReader("a1.txt"));


        // Line will hold our line read from the file
        String line = "";
        String line1 = "";


        // The counter will keep track of how many lines we have read

        int counter = 0;
        int counter1 = 0;


        // Read in a line from the file and store it in "line". Do this while we don't hit null or while the counter is less than 4.

        // The counter prevents us from reading in too many lines.

        while (((line = reader.readLine()) != null) && (counter < 4)) {

            as[counter] = line;

            counter++;

        }

        while (((line1 = reader1.readLine()) != null) && (counter1 < 16)) {

            bs[counter1] = line1;

            counter1++;

        }

         System.out.println("value"+as[0]);
          System.out.println("value"+bs[0]);
       int temp,temp1,j;
       temp=as.length;
       temp1=bs.length;
       System.out.println("length:"+temp);
       System.out.println("length1:"+temp1);
       for(i=0;i<bs.length;i++)
       {
           for(j=0;j<as.length;j++)
           {
               if(as[j].equals(bs[i]))
               {
                   //ignore
               }
               else
               {
                   temp++;
                   as[temp]=bs[i];

               }
           }

       }


        // With a foreach style loop we loop through the array of strings and print them out to show they were read in.



        reader1.close(); 
        reader.close();

    }

    catch (Exception ex) { System.out.println("Exception: " + ex.getMessage()); }

    }
}

【问题讨论】:

  • 你不清楚,update the array string as[ ] with the new strings present in bs[ ]是什么意思
  • 如果您尝试追加到数组,这在 Java 中不起作用。数组是固定长度的。
  • // Create an array of 4 strings (indexes 0 - 3): 别忘了更新你的 cmets

标签: java arrays


【解决方案1】:

由于您使用两个数组只包含字符串,最好将两者都转换为列表并添加

List aList = (Arrays.asList(as));
List bList = (Arrays.asList(bs));
bList.removeAll(aList); // assuming you have some common objects in both
aList.addAll(bList);
as = aList.toArray(); // Convert back to array

【讨论】:

  • 我收到“不支持的操作异常”
  • 能否请您分享您的代码以及异常
  • bList.removeAll(aList); aList.addAll(bList); as = (String[]) aList.toArray(as); for (aList a:list) { System.out.println(a);我得到不受支持的操作异常。如何打印 arraylist 的内容?
  • 你应该使用 String[] as = (String[])aList.toArray();
【解决方案2】:

看看 Apache Commons ArrayUtils: 您可以使用 contains 和第三个临时数组的组合来存储差异(即 !contains)。

谢谢。

【讨论】:

    【解决方案3】:
    else
    {
        temp++;
        as[temp]=bs[i];
    }
    

    正如 Thilo 在 cmets 中所说,这在 Java 中不起作用。一旦设置了数组的大小,就不能增加数组的大小。

    我建议使用ArrayList 而不是array。您可以简单地将新项目添加到数组列表中而没有任何问题。

    如果您坚持使用arrays,您可以创建一个更长的新数组并将旧数组复制到此处并添加新元素。我不会推荐这个。

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2019-04-08
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2012-08-07
      相关资源
      最近更新 更多