Java 如何使用for循环比较同一字符串的每个字符?

Java 如何使用for循环比较同一字符串的每个字符?,java,Java,因此,我必须制作一个程序,在这个程序中,我必须比较字符串的每个字符,看看它们是否在字符串中出现不止一次,如果它们出现不止一次,那么我必须从数组列表中删除该字符串,例如“peer”和“pear”,然后它应该删除“peer”,因为“peer”中有两个e,那么我该怎么做呢?这里有一个提示。要检查是否存在任何重复字符,可以使用集 For each character of String check if the char is in the set if yes the

因此,我必须制作一个程序,在这个程序中,我必须比较字符串的每个字符,看看它们是否在字符串中出现不止一次,如果它们出现不止一次,那么我必须从数组列表中删除该字符串,例如“peer”和“pear”,然后它应该删除“peer”,因为“peer”中有两个e,那么我该怎么做呢?

这里有一个提示。要检查是否存在任何重复字符,可以使用

For each character of String
    check if the char is in the set
    if yes
         then you can delete this String from arraylist and stop checking this string
    else
         add this char to the set
试试这个:
if(Pattern.matches(“\w{2,}”,ID)==true){//do your want}
。然后,我希望你知道如何删除字符串。

Pythonic答案:
这是无效的Java syntaxi,有点修复了它
charlist=['a','b','c','d','e','f','g','h','i','j','k','p','r']
stringlist=['peer','pear']
for string in stringlist:
  for character in charlist:
    if string.count(character) > 1:
      stringlist.remove(string)
print (stringlist)