繁体   English   中英

如何在java中的给定文本中搜索最大大小的单词?

[英]How to search the word with max size in a given text in java?

如果给出了一些文本,我需要搜索最大长度的单词。所以我是这样想的。

public class Foo {

    public static void main(String[] args) {

        String str = "java string split method by javatpoint";

        String words[] = str.split("\\s");//splits based on white space

        for(String w:words) {
            //System.out.println(w);

            String temp = w;
            if(temp.length()<w.length()) {
                String maxLengthString = w;
                System.out.println("maxLengthString is w");
                }else {
                    System.out.println("maxLengthString is temp");
                }
            }
    }
}

将最长的存储在 temp 中,并且仅(尝试)在循环后打印它。 每次遇到较长的单词时重新分配 temp。

 for(String w:words) {
            //System.out.println(w);

            String temp = w;
            if(temp.length()<w.length()) {
                String maxLengthString = w;
                System.out.println("maxLengthString is w");
                }else {
                    System.out.println("maxLengthString is temp");
                }
            }

这将打印每次迭代,这不是您想要的。 为了保留最长的单词,您需要在循环外声明 temp 。

 String temp = "";
 for(String w:words) {
  // this will override temp only if the word is longer
  if ( w.length() > temp.length() ) {
    temp = w;
  }
 }
 // don't print harcoded "temp", but concatenate the value of the variable temp
 System.out.println("The longest word is: " + temp);

初始化一个长度为 0的字符串,并将其与数组中的每个字符串进行比较,并更新每次迭代中找到的maxString ,最后您将获得 maxString。

public class Foo {
    public static void main(String[] args) {

        String str = "java string split method by javatpoint";

        String words[] = str.split("\\s");//splits based on white space
        String maxString = "";
        for(String w:words) {            
            if(w.length() > maxString.length()) {
                maxString = w;
            }
        }

        System.out.println(maxString);
}

或者,您可以使用相同的streams

    String str = "string split method by javatpoint javatpoint";

    String words[] = str.split("\\s");//splits based on white space
    List<String> names = Arrays.asList(words);

    Optional<String> result = 
    names.stream().max(Comparator.comparingInt(String::length));
    System.out.println(result.get());

嘿,请阅读代码中的注释,这将有助于理解发生了什么。

    public static void main(String[] args) {
         String str = "java string split method by javatpoint";

        String words[] = str.split("\\s");//splits based on white space

        int max =0;
        String maxWord = "";

        for(String w:words) {
            max = maxWord.length(); /* assigning the length of maxWord on every iteration since it might change */
            int mightBeNewMax = w.length(); /* always instantiating a newlenght that might be greater than previous, also this saves us the recreation below incase we wan to assign it as the new max length*/
            if(max<mightBeNewMax) {/* here we simply check the previous max against the new one */

/* getting to this block means we have a new max length so we simply update our initially declared variables above. */
                maxWord = w; 
                max = mightBeNewMax;
            }
        }

//Below is just a concatenated strings to make sense out of our code.
        System.out.println("maxLengthString is: "+ maxWord + " With length of: "+max);
    }

注意:我删除了你的 else 语句,因为当我们循环的新字符串小于前一个字符串时我们不想做任何事情,所以我们只关心新的更长的字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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