文档

Java™ 教程-Java Tutorials 中文版
Trail: Learning the Java Language
Lesson: Numbers and Strings
主页>学习 Java 语言>数字和字符串

问题和练习的答案:字符和字符串

问题

  1. 问题:以下 string builder 的初始容量是多少?

    StringBuilder sb = new StringBuilder("Able was I ere I saw Elba.");
    

    答案:初始字符串的长度+ 16:26 + 16 = 42。

  2. 请考虑以下字符串:

    String hannah = "Did Hannah see bees? Hannah did.";
    
    1. 问题:表达式 hannah.length() 显示的值是多少?

      答案:32.

    2. 问题:方法调用 hannah.charAt(12) 返回的值是多少?

      答案:e.

    3. 问题:写一个表达式,引用 hannah 引用的字符串中的字母 b

      答案:hannah.charAt(15).

  3. 问题:下面的表达式返回的字符串有多长?该字符串是什么?

    "Was it a car or a cat I saw?".substring(9, 12)
    

    答案:长度为 3 个字符:car。它不包括 car 后的空间。

  4. 问题:在以下程序中,名为 ComputeResult,每个编号行执行后 result 的值是多少?

    public class ComputeResult {
        public static void main(String[] args) {
            String original = "software";
            StringBuilder result = new StringBuilder("hi");
            int index = original.indexOf('a');
    
    /*1*/   result.setCharAt(0, original.charAt(0));
    /*2*/   result.setCharAt(1, original.charAt(original.length()-1));
    /*3*/   result.insert(1, original.charAt(4));
    /*4*/   result.append(original.substring(1,4));
    /*5*/   result.insert(3, (original.substring(index, index+2) + " ")); 
    
            System.out.println(result);
        }
    }
    

    答案:

    1. si
    2. se
    3. swe
    4. sweoft
    5. swear oft

练习

  1. 练习:显示两种方法将以下两个字符串连接在一起以获取字符串 "Hi, mom."

    String hi = "Hi, ";
    String mom = "mom.";
    

    答案:hi.concat(mom)hi + mom

  2. 练习:编写一个程序,用你的全名计算首字母并显示它们。

    答案:ComputeInitials

    
    public class ComputeInitials {
        public static void main(String[] args) {
            String myName = "Fred F. Flintstone";
            StringBuffer myInitials = new StringBuffer();
            int length = myName.length();
    
            for (int i = 0; i < length; i++) {
                if (Character.isUpperCase(myName.charAt(i))) {
                    myInitials.append(myName.charAt(i));
                }
            }
            System.out.println("My initials are: " + myInitials);
        }
    }
    
  3. 练习:变位词(anagram)是通过转换另一个单词或短语的字母而形成的单词或短语;例如,"parliament" 是 "partial men" 的变位词,"software" 是 "swear oft" 的变位词。编写一个程序,确定一个字符串是否是另一个字符串的变位词。该程序应忽略空格和标点符号。

    答案:Anagram

     
    
    public class Anagram {
    
        public static boolean areAnagrams(String string1,
                                          String string2) {
    
            String workingCopy1 = removeJunk(string1);
            String workingCopy2 = removeJunk(string2);
    
    	    workingCopy1 = workingCopy1.toLowerCase();
    	    workingCopy2 = workingCopy2.toLowerCase();
    
    	    workingCopy1 = sort(workingCopy1);
    	    workingCopy2 = sort(workingCopy2);
    
            return workingCopy1.equals(workingCopy2);
        }
    
        protected static String removeJunk(String string) {
            int i, len = string.length();
            StringBuilder dest = new StringBuilder(len);
      		char c;
    
    	    for (i = (len - 1); i >= 0; i--) {
    	        c = string.charAt(i);
    	        if (Character.isLetter(c)) {
    		        dest.append(c);
    	        }
    	    }
    
            return dest.toString();
        }
    
        protected static String sort(String string) {
    	    char[] charArray = string.toCharArray();
    
    	    java.util.Arrays.sort(charArray);
    
            return new String(charArray);
        }
    
        public static void main(String[] args) {
            String string1 = "Cosmo and Laine:";
            String string2 = "Maid, clean soon!";
    
            System.out.println();
            System.out.println("Testing whether the following "
                             + "strings are anagrams:");
            System.out.println("    String 1: " + string1);
            System.out.println("    String 2: " + string2);
            System.out.println();
    
            if (areAnagrams(string1, string2)) {
                System.out.println("They ARE anagrams!");
            } else {
                System.out.println("They are NOT anagrams!");
            }
            System.out.println();
        }
    }
    

Previous page: Questions and Exercises: Characters and Strings