简体   繁体   English

如何从ArrayList(Java)打印最短的字符串

[英]How to print shortest String from ArrayList (Java)

Ok so the goal is to print the smallest length string ( for example, if input is "co", "college", "colleges", "university" I want it to print out co. I've tried the college.compareTo( ____ ); as well as a couple other things but I can't seem to get it to print it out. Also it isn't anything fancy (if you've read/have head first java 2nd edition then we are on chapters 5/6) I'd rather if you can link me to a video that will show the process of what needs to be done with an explanation but anything is helpful :s been staring at this coding for a couple weeks and kinda went brain dead on it... This is what I have so far (accepts strings from user); 好的,所以目标是打印最小长度的字符串(例如,如果输入为“ co”,“ college”,“ colleges”,“ university”,我希望将其打印为co。我已经尝试了college.compareTo( ____);以及其他一些东西,但我似乎无法将其打印出来。这也不是什么花哨的东西(如果您已经读过Java 2nd第一版,那么我们将在第5章学习) / 6)如果您可以将我链接到一个视频,该视频将显示需要解释的过程,但有帮助的话:一直盯着该编码工作了几周,有点不知所措到目前为止,这是我所拥有的(接受来自用户的字符串);

ArrayList <String> colleges = new ArrayList <String> ( ) ;
    String input;
    Scanner scan = new Scanner(System.in) ;
    while (true) {
        System.out.println("Please enter your college (Press Enter twice to quit) ");
        input = scan.nextLine();
        if (input.equals ("")) {
            break;
        }
        else {
            colleges.add(input.toUpperCase () );
        }// end of if else statement
    }// end of while loop

    System.out.println("The following " + colleges.size() + " colleges have been entered:");

    for ( String college : colleges) {
         System.out.println("\n" + college );
         System.out.println("Character count: " +  college.length( ) );
    }// end of for each loop

These are the steps you need: 这些是您需要的步骤:

  1. Create a custom Comparator<String> to sort strings based on their length. 创建一个自定义的Comparator<String>以根据字符串的长度对其进行排序。
  2. Get the shortest string from your list by using your custom comparator with the Collections.min() method. 通过将您的自定义比较器与Collections.min()方法结合使用,从列表中获取最短的字符串。

In its most compact version, your code could look something like this (assuming there are no null strings in the list): 在最紧凑的版本中,您的代码可能类似于以下内容(假设列表中没有null字符串):

String shortest = Collections.min(colleges, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        return s1.length() - s2.length();
    }
});

You can use the following logic to print the smallest string in given arrayList 您可以使用以下逻辑在给定的arrayList中打印最小的字符串

string smallString = "";
for ( String college : colleges) {
    if(smallString.length() == 0 && college.length() != 0)
    {
        smallString = college ;
    }
    else if(college.length() < smallString.length() && college.length() != 0)
    {
        smallString = college;
    }
}
println("Smallest string is: " + smallString );
public static String SmallestString(ArrayList <String> collegeArray){
    String smallest = "";
    System.out.println("");
    for(String string :collegeArray){
        //if input-string is null or empty
        if(string == null || string.trim() == "" || string.trim().length()==0){
            System.out.println("Emtpy String Encountered");
            continue;
        }
        if(smallest == ""){
            smallest = string;
            continue;
        }
        if(string.length() < smallest.length()){
            smallest = string;
        }
    }
    return smallest;
}

Call it with: System.out.println("\\nSmallest String: " + SmallestString(colleges)); 调用它: System.out.println("\\nSmallest String: " + SmallestString(colleges));

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

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