【问题标题】:Static method that takes an array of strings and returns an integer采用字符串数组并返回整数的静态方法
【发布时间】:2019-02-23 19:22:36
【问题描述】:

我对编程还是很陌生,我正在尝试做这个小程序,我在其中编写了一个静态方法,该方法接受一个字符串数组并返回一个整数。从那里我必须找到最小/最短字符串的索引位置并返回该索引值。我完全迷失了,并为此苦苦挣扎。我能得到一些帮助吗?

到目前为止我的代码...

public class test 
{

    public static void main(String [] args) 
    {
      String [] Month = {"July", "August", "September", "October"};
        smallest(Month);
        System.out.println("The shortest word is " + smallest(Month));
    }

    public static String smallest(String Month[]) 
    {
        String first = Month[0];
        for (int i = 1 ; i < Month.length ; i++) 
        {
            if (Month[i].length()<first.length())
             {
                first = Month[i];
            } 
        } 
        return first;
    }
}

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 可以分享代码吗?

标签: java return


【解决方案1】:

检查下面的代码,

public class Test {
    public static void main(String [] args) 
    {
      String [] month = {"July", "August", "September", "October"};
     //   smallest(Month);
        System.out.println("The shortest word index position is " + smallest(month));
    }

    public static int smallest(String Month[]) 
    {
        String first = Month[0];
        int position=0;
        for (int i = 1 ; i < Month.length ; i++) 
        {
            if (Month[i].length()<first.length())
             {
                first = Month[i];
                position=i;
            } 
        } 
        return position;
    }

}

【讨论】:

    【解决方案2】:

    您的代码实际上非常接近,但是--如果我正确理解您的任务--而不是实际的最小元素,您应该只跟踪索引,因为这是您最终想要返回的内容。

    public static int smallest(String[] months) {
        int first = 0;
        for (int i = 1; i < months.length; i++) {
            if (months[i].length() < months[first].length()) {
                first = i;
            } 
        } 
        return first;
    }
    

    【讨论】:

      【解决方案3】:

      专注于smallest 方法。

      public static void smallest(String[] month) {
          // since we have no knowledge about the array yet, lets
          // say that the currently known shortest string has
          // size = largest possible int value java can store 
          int min_length = Integer.MAX_INT, min_length_idx = -1;
          for (int i = 0; i < month.length; i++) {
              // is this current string a candidate for a new minimum?
              if (month[i].length() < min_length) {
                  // if so, lets keep track of the length so that future
                  // indices can be compared against it
                  min_length = month[i].length();
                  min_length_idx = i;
              }
          } 
          return min_length_idx;
      }
      

      此方法还将涵盖数组中没有任何字符串的情况,即空数组。

      【讨论】:

        【解决方案4】:
        public static int smallest(String month[]) {
           int smallest = 0;
           for ( int i=0; i<month.length; i++ {
             if (...) {
               smallest = i;
             }
           }
           return smallest;
        }
        

        注意:使用标准约定,变量名称以小写字母开头。

        【讨论】:

          猜你喜欢
          • 2015-09-17
          • 1970-01-01
          • 2021-04-26
          • 2013-11-03
          • 1970-01-01
          • 1970-01-01
          • 2015-06-09
          • 2013-02-27
          • 1970-01-01
          相关资源
          最近更新 更多