Java 如何仅打印数组中具有重复字符的单词?

Java 如何仅打印数组中具有重复字符的单词?,java,arrays,Java,Arrays,我只想打印具有数组中重复字符的单词。例如,下面的代码打印整个数组,但如果我只希望它打印“hello”,因为它有一个重复字符或任何其他具有重复字符的单词,而不输出没有重复字符的单词,该怎么办 public class iterateThruArray { public static void main(String[] args) { String[] words = {"hello", "lmao", "why"}; int len = word

我只想打印具有数组中重复字符的单词。例如,下面的代码打印整个数组,但如果我只希望它打印“hello”,因为它有一个重复字符或任何其他具有重复字符的单词,而不输出没有重复字符的单词,该怎么办

public class iterateThruArray
{
    public static void main(String[] args) 
    {
        String[] words = {"hello", "lmao", "why"};
        int len = words.length;

        for(int i = 0; i < len; i++)
        {
            String word = words[i];
            //prints array words
            System.out.println(word);
        }

    }
}
公共类迭代数组
{
公共静态void main(字符串[]args)
{
String[]words={“hello”、“lmao”、“why”};
int len=单词长度;
对于(int i=0;i
现在,您可以调用hasRepeatingChars(单词[i])来自循环,仅当返回true时才打印

    public boolean hasRepeatingChars(String word) {
        char previous = '%'; // some char you know won't be in the string

        for (int i = 0; i < word.length(); i++) {
            char current = word.charAt(i);
            if (current == previous) {
                return true;
            }
        }

        return false;
    }
公共布尔hasRepeatingChars(字符串字){
char previous='%';//您知道的某些字符不在字符串中
for(int i=0;i

编辑:我现在看到有人在上面评论你自己解决这个问题。。。别偷看

现在,您可以调用hasRepeatingChars(words[i])来自循环,仅当返回true时才打印

    public boolean hasRepeatingChars(String word) {
        char previous = '%'; // some char you know won't be in the string

        for (int i = 0; i < word.length(); i++) {
            char current = word.charAt(i);
            if (current == previous) {
                return true;
            }
        }

        return false;
    }
公共布尔hasRepeatingChars(字符串字){
char previous='%';//您知道的某些字符不在字符串中
for(int i=0;i
编辑:我现在看到有人在上面评论你自己解决这个问题。。。别偷看

试试下面的方法

 public class iterateThruArray
    {
        public static boolean distinct(String word)
        {
            char[] ch = word.toCharArray();
            Arrays.sort(ch);
            for (int i = 0; i < ch.length - 1; i++) 
            { 
                //After sorting the array, all repeating characters(if any) would have to appear adjacent to each other, hence we just check for repeating adjacent characters
                if (ch[i] == ch[i + 1]) 
                    return false; 
            } 
            return true;   
        }

        public static void main(String[] args) 
        {
            String[] words = {"hello", "lmao", "why"};
            int len = words.length;

            for(int i = 0; i < len; i++)
            {
                String word = words[i];
                if(distinct(word))
                    System.out.println(word);
            }

        }
    }
公共类迭代数组
{
公共静态布尔值不同(字符串字)
{
char[]ch=word.toCharArray();
数组。排序(ch);
对于(int i=0;i
尝试以下操作

 public class iterateThruArray
    {
        public static boolean distinct(String word)
        {
            char[] ch = word.toCharArray();
            Arrays.sort(ch);
            for (int i = 0; i < ch.length - 1; i++) 
            { 
                //After sorting the array, all repeating characters(if any) would have to appear adjacent to each other, hence we just check for repeating adjacent characters
                if (ch[i] == ch[i + 1]) 
                    return false; 
            } 
            return true;   
        }

        public static void main(String[] args) 
        {
            String[] words = {"hello", "lmao", "why"};
            int len = words.length;

            for(int i = 0; i < len; i++)
            {
                String word = words[i];
                if(distinct(word))
                    System.out.println(word);
            }

        }
    }
公共类迭代数组
{
公共静态布尔值不同(字符串字)
{
char[]ch=word.toCharArray();
数组。排序(ch);
对于(int i=0;i
您可以使用32位检查器检查字符串的每个字母。如果检查程序的“和”操作大于0,则存在重复

代码如下所示:

  boolean IsUniqueString (string input)
 {

    int checkValue = 0; 

    for (int temp = 0; temp < str.length(); temp++) { 

        int bitIndex = input.charAt(temp) - 'a’;

        if ((checkValue & (1 << bitIndex)) > 0) 

            return false; 

        checkValue= checkValue | (1 << bitIndex); 

    return true; 
  } 
boolean IsUniqueString(字符串输入)
{
int checkValue=0;
对于(int-temp=0;tempcheckValue=checkValue |(1您可以使用32位检查器检查字符串的每个字母。如果检查器的“and”运算大于0,则存在重复

代码如下所示:

  boolean IsUniqueString (string input)
 {

    int checkValue = 0; 

    for (int temp = 0; temp < str.length(); temp++) { 

        int bitIndex = input.charAt(temp) - 'a’;

        if ((checkValue & (1 << bitIndex)) > 0) 

            return false; 

        checkValue= checkValue | (1 << bitIndex); 

    return true; 
  } 
boolean IsUniqueString(字符串输入)
{
int checkValue=0;
对于(int-temp=0;temp你认为这个解决方案怎么样

    public static void main(String[] args) 
    {
        // some words ..
        String[] words = {"hello", "lmao", "why", "1abc1", "letter", "middle"};

        int len = words.length;

        for(int i = 0; i < len; i++) {
            String word = words[i];
            if(hasRepeatingChars(word)) {
                System.out.println(word);
            }
        }

    }

    public static boolean hasRepeatingChars(String word) {
        // here we will hold the characters and count for double letters
        LinkedList<Character> ll = new LinkedList<>();

        for (int i = 0; i < word.length(); i++) {           
            // we iterate now through the letters and get the current letter
            Character current = word.charAt(i);
            // we add here the current letter to the list
            ll.add(current);

            // when the position is at the first letter do not go into this block
            if(i > 0) {
                // ok from the second letter on we check the neighbours :)
                Character neighbour1 = ll.get(ll.size() -1);
                Character neighbour2 = ll.get(ll.size() -2);

                // .. if they are equal return true
                if(neighbour1.equals(neighbour2)) {
                    return true;
                }

            }

        }

        return false;
    }
publicstaticvoidmain(字符串[]args)
{
//有些话。。
String[]words={“hello”、“lmao”、“why”、“1abc1”、“letter”、“middle”};
int len=单词长度;
对于(int i=0;i0){
//好的,从第二封信开始,我们检查邻居:)
字符邻域1=ll.get(ll.size()-1);
字符邻域2=ll.get(ll.size()-2);
//…如果它们相等,则返回true
if(邻域1.等于(邻域2)){
返回t