简体   繁体   中英

Loop through array inside array

I have 2 Arrays: one contains words ( words_array ) and the other contains letters ( letters_array )

I loop through the words_array and inside the loop I loop through the letters array and in the second loop I make a condition "if the word contain a letter in the letters array divide it by some way if else it should print the word as it is " but it doesn't work because it prints duplicated words.

Here is my code:

for (int i = 0; i < words_array.length; i++) {
    for (int j = 0; j < letters_array.length ;j++ ) {
        if (arryWords[i].contains(letters[j]+"")) {
            int wordLength = arryWords[i].length();
            int letterIndex = arryWords[i].indexOf(letters[j]);
            String sub = arryWords[i].substring(0, letterIndex + 1);
            System.out.println(sub);
            System.out.println(arryWords[i].substring(letterIndex + 1, wordLength));
        } else if (!arryWords[i].contains(letters[j] + "")) {
            System.out.println(arryWords[j]);
        }
    } 
}

but the actual result is:

hello
hello
hello
he
llo

I want, if the word doesn't have any letters in the letters_array , it to print only one time and if the word has a letters in the letters_array it should divide two 2 part in the location of founded letter as I make in my code.

Thanks.

I renamed letters_array to lettersArr and words_array to wordsArr but I believe you want a Set of String(s); for example,

// TreeSet would be ordered.
Set<String> wordSet = new HashSet<String>(); // The set of words.
for (int i = 0; i < wordsArr.length; i++) {
  final String word = wordsArr[i]; // local copy of the word.
  for (int j = 0; j < lettersArr.length; j++) {
    int pos = word.indexOf(lettersArr[j]); // get the position of the letter.
    if (pos > -1) { // if the word contains the letter.
      final String first = word.substring(0, pos)
          .trim();
      final String second = word.substring(
          pos + 1, word.length()).trim();
      if (first.length() > 0) {
        wordSet.add(first); // add before the letter.
      }
      if (second.length() > 0) {
        wordSet.add(second); // add after the letter.
      }
    } else {
      wordSet.add(word); // add the word.
    }
  }
}
System.out.println(wordSet); // display the result.

The most concise way is to split the string by Regex:

public static void main(String... args){
    String[] word_array = {"blahXblahX", "blahYblahY", "blahZblahZ"};
    char[] letters_array = {'X','Z'};

    String pattern = "(?<=[" + new String(letters_array) + "])";
    for(int i=0; i<word_array.length; i++)
        for(String str: word_array[i].split(pattern))
            System.out.println(str);
}

Output:

blahX
blahX
blahYblahY
blahZ
blahZ

Explanation:

For this example pattern will have the form (?<=[XZ]) which matches on the delimiters X and Z but will not consume the delimiters . For example:

// consumes delimiter: ["blah","blah"]
"blahXblahX".split("[XZ]");

// keeps delimiter: ["blahX","blahX"]
"blahXblahX".split("(?<=[XZ])");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM