xiachannnn 2022-03-22 17:41 采纳率: 0%
浏览 31

阿里巴巴Java机试题

阿里巴巴机试题,根据注释里的方法调用的结果,写全方法的实现代码,一共四个方法

public class StringUtils {
    /**
     * StringUtils.join(null, *) = null 
     * StringUtils.join([], *) = ""
     * StringUtils.join([null], *) = "" 
     * StringUtils.join(["a", "b", "c"], ';') = "a;b;c" 
     * StringUtils.join(["a", "b", "c"], null) = "abc"
     * StringUtils.join([null, "", "a"], ';') = ";;a"
     */
     public static String join(Object[] array, char separator) {

         return null;
     }
     /**
     * <p>Left pad a String with a specified character.</p>
     *
     * <p>Pad to a size of {@code size}.</p>
     *
     * <pre>
     * StringUtils.leftPad(null, *, *)     = null
     * StringUtils.leftPad("", 3, 'z')     = "zzz"
     * StringUtils.leftPad("bat", 3, 'z')  = "bat"
     * StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
     * StringUtils.leftPad("bat", 1, 'z')  = "bat"
     * StringUtils.leftPad("bat", -1, 'z') = "bat"
     * </pre>
     *
     * @param str  the String to pad out, may be null
     * @param size  the size to pad to
     * @param padChar  the character to pad with
     * @return left padded String or original String if no padding is necessary,
     *  {@code null} if null String input
     * @since 2.0
     */
    public static String leftPad(String str, int size, String padChar) {
    
       return null;
    }
    
    /**
     * <p>Right pad a String with a specified character.</p>
     *
     * <p>The String is padded to the size of {@code size}.</p>
     *
     * <pre>
     * StringUtils.rightPad(null, *, *)     = null
     * StringUtils.rightPad("", 3, 'z')     = "zzz"
     * StringUtils.rightPad("bat", 3, 'z')  = "bat"
     * StringUtils.rightPad("bat", 5, 'z')  = "batzz"
     * StringUtils.rightPad("bat", 1, 'z')  = "bat"
     * StringUtils.rightPad("bat", -1, 'z') = "bat"
     * </pre>
     *
     * @param str  the String to pad out, may be null
     * @param size  the size to pad to
     * @param padChar  the character to pad with
     * @return right padded String or original String if no padding is necessary,
     *  {@code null} if null String input
     * @since 2.0
     */
    public static String rightPad(String str, int size, String padChar) {
  
       return null;
    }
    /**
     * <p>Capitalizes a String changing the first letter to title case as
     * per {@link Character#toTitleCase(char)}. No other letters are changed.</p>
     *
    
     * <pre>
     * StringUtils.capitalize(null)  = null
     * StringUtils.capitalize("")    = ""
     * StringUtils.capitalize("cat") = "Cat"
  * StringUtils.capitalize("cAt") = "CAt"
     * </pre>
     *
     * @param str the String to capitalize, may be null
     * @return the capitalized String, {@code null} if null String input
     * @since 2.0
     */
    public static String capitalize(String str) {
 
      return null;
    }
    public static void main(String[] args) {
        
      }
}

  • 写回答

2条回答 默认 最新

  • zcrazy胡说八道 2022-03-23 09:50
    关注
    
    public class StringUtils {
        /**
    
         * StringUtils.join(null, *) = null
    
         * StringUtils.join([], *) = ""
    
         * StringUtils.join([null], *) = ""
    
         * StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
    
         * StringUtils.join(["a", "b", "c"], null) = "abc"
    
         * StringUtils.join([null, "", "a"], ';') = ";;a"
    
         */
        // FIXME 这里面第二个参数如果是char,根本传不进去null,所以改成Character
        public static String join(Object[] array, Character separator) {
            if(null != array) {
                if(array.length==0) return "";
                StringBuilder sb = new StringBuilder();
                boolean hasSep = false;
                for (Object a : array) {
                    if (null != a) {
                        sb.append(a);
                    }
                    if (null != separator) {
                        hasSep = true;
                        sb.append(separator);
                    }
                }
                if(hasSep) {
                    return sb.substring(0, sb.length() - 1);
                } else{
                    return sb.toString();
                }
            }
            return null;
        }
    
        /**
    
         * <p>Left pad a String with a specified character.</p>
    
         *
    
         * <p>Pad to a size of {@code size}.</p>
    
         *
    
         * <pre>
    
         * StringUtils.leftPad(null, *, *)     = null
    
         * StringUtils.leftPad("", 3, 'z')     = "zzz"
    
         * StringUtils.leftPad("bat", 3, 'z')  = "bat"
    
         * StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
    
         * StringUtils.leftPad("bat", 1, 'z')  = "bat"
    
         * StringUtils.leftPad("bat", -1, 'z') = "bat"
    
         * </pre>
    
         *
    
         * @param str  the String to pad out, may be null
    
         * @param size  the size to pad to
    
         * @param padChar  the character to pad with
    
         * @return left padded String or original String if no padding is necessary,
    
         *  {@code null} if null String input
    
         * @since 2.0
    
         */
    
        public static String leftPad(String str, int size, String padChar) {
            if(null == str) return null;
    
            if(str.length()>=size) return str;
            else{
                StringBuilder sb = new StringBuilder();
                for(int i=0;i<size-str.length();i++){
                    sb.append(padChar);
                }
                sb.append(str);
                return sb.toString();
            }
        }
    
    
    
        /**
    
         * <p>Right pad a String with a specified character.</p>
    
         *
    
         * <p>The String is padded to the size of {@code size}.</p>
    
         *
    
         * <pre>
    
         * StringUtils.rightPad(null, *, *)     = null
    
         * StringUtils.rightPad("", 3, 'z')     = "zzz"
    
         * StringUtils.rightPad("bat", 3, 'z')  = "bat"
    
         * StringUtils.rightPad("bat", 5, 'z')  = "batzz"
    
         * StringUtils.rightPad("bat", 1, 'z')  = "bat"
    
         * StringUtils.rightPad("bat", -1, 'z') = "bat"
    
         * </pre>
    
         *
    
         * @param str  the String to pad out, may be null
    
         * @param size  the size to pad to
    
         * @param padChar  the character to pad with
    
         * @return right padded String or original String if no padding is necessary,
    
         *  {@code null} if null String input
    
         * @since 2.0
    
         */
    
        public static String rightPad(String str, int size, String padChar) {
    
            if(null == str) return null;
    
            if(str.length()>=size) return str;
            else{
                StringBuilder sb = new StringBuilder();
                sb.append(str);
                for(int i=0;i<size-str.length();i++){
                    sb.append(padChar);
                }
                return sb.toString();
            }
        }
    
        /**
    
         * <p>Capitalizes a String changing the first letter to title case as
    
         * per {@link Character#toTitleCase(char)}. No other letters are changed.</p>
    
         *
    
    
    
         * <pre>
    
         * StringUtils.capitalize(null)  = null
    
         * StringUtils.capitalize("")    = ""
    
         * StringUtils.capitalize("cat") = "Cat"
    
         * StringUtils.capitalize("cAt") = "CAt"
    
         * </pre>
    
         *
    
         * @param str the String to capitalize, may be null
    
         * @return the capitalized String, {@code null} if null String input
    
         * @since 2.0
    
         */
    
        public static String capitalize(String str) {
            if(null==str) return null;
            if(str.length()==0) return "";
            char[] cs=str.toCharArray();
            if(cs[0]>96&&cs[0]<123)
                cs[0]-=32;
            return String.valueOf(cs);
        }
    
        public static void main(String[] args) {
            System.out.println(StringUtils.join(null,'*'));
    
            System.out.println(StringUtils.join(new String[]{}, '*'));
    
            System.out.println(StringUtils.join(new String[]{null}, '*'));
    
            System.out.println(StringUtils.join(new String[]{"a", "b", "c"}, ';'));
    
            System.out.println(StringUtils.join(new String[]{"a", "b", "c"}, null));
    
            System.out.println(StringUtils.join(new String[]{null, "", "a"}, ';'));
    
            System.out.println(StringUtils.leftPad(null, 100, "*"));
    
            System.out.println(StringUtils.leftPad("", 3, "z"));
    
            System.out.println(StringUtils.leftPad("bat", 3, "z"));
    
            System.out.println(StringUtils.leftPad("bat", 5, "z"));
    
            System.out.println(StringUtils.leftPad("bat", 1, "z"));
    
            System.out.println(StringUtils.leftPad("bat", -1, "z"));
    
            System.out.println(StringUtils.rightPad(null, 100, "*"));
    
            System.out.println(StringUtils.rightPad("", 3, "z"));
    
            System.out.println(StringUtils.rightPad("bat", 3, "z"));
    
            System.out.println(StringUtils.rightPad("bat", 5, "z"));
    
            System.out.println(StringUtils.rightPad("bat", 1, "z"));
    
            System.out.println(StringUtils.rightPad("bat", -1, "z"));
    
            System.out.println(StringUtils.capitalize(null));
    
            System.out.println(StringUtils.capitalize(""));
    
            System.out.println(StringUtils.capitalize("cat"));
    
            System.out.println(StringUtils.capitalize("cAt"));
    
        }
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月22日

悬赏问题

  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,
  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。