gpt4 book ai didi

java - list.removeAll() 的问题

转载 作者:行者123 更新时间:2023-12-01 17:19:36 26 4
gpt4 key购买 nike

我使用数组列表来查找两个字符串(即 str2str3)之间的差异。当我使用下面的代码时,它工作正常并返回预期的输出。但当我更换时

str2 = #19, 6th cross, 7th main road, townname, cityname-560036
str3 = #19, 6th cross, 17th main road, townname, cityname-560036

预期输出应该是:al1的内容:[1]

但我得到的输出是:al1的内容:[]

谁能解释一下我哪里出错了?

提前致谢。

这是我的 doPost 方法:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
str2="Hello";
str3="Hallo";
System.out.println("-------");
System.out.println("This is first string:"+""+str2);
System.out.println("This is second string:"+""+str3);


ArrayList al = new ArrayList();
ArrayList al1=new ArrayList();

System.out.println("Initial size of al: " + al.size());

// add elements to the array list
for (int i = 0;i < str2.length(); i++)
{
al.add(str2.charAt(i));
}
System.out.println("Size of al after additions: " + al.size());

// display the array list
System.out.println("Contents of al: " + al);



for (int i = 0;i < str3.length(); i++)
{
al1.add(str3.charAt(i));
}

System.out.println("Contents of al1: " + al1);

System.out.println("Size of al1 after additions: " + al1.size());
boolean hg=al1.removeAll(al);
System.out.println("Remove All:"+hg);


System.out.println("Contents of al: " + al);
System.out.println("Contents of al1: " + al1);

}

最佳答案

这里“#19”已经有了字符“1”,因此当您调用“全部删除”时,所有“1”都会被删除,包括您希望保留的那个。我认为你应该迭代字符并提取差异,否则比较不会很准确。

关于java - list.removeAll() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19680572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com