我正在使用 arraylists 来查找两个字符串之间的差异,str2
即str3
. 当我使用下面的代码时,它可以完美运行并返回预期的输出。但是当我更换
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);
}