《Java语言程序设计(基础篇)》(第10版梁勇著)第二十一章练习题答案.docxVIP

  • 53
  • 0
  • 约3万字
  • 约 29页
  • 2021-10-27 发布于天津
  • 举报

《Java语言程序设计(基础篇)》(第10版梁勇著)第二十一章练习题答案.docx

《 Java 语言程序设计(基础篇) 》 (第 10 版 梁勇 著) 第二十一章 练习题答案 21.1 import java.util.*; public class Exercise21_01 { public static void main(String[] args) { LinkedHashSetString set1 = new LinkedHashSetString(Arrays.asList( new String[]{ George , Jim , John , Blake , Kevin , Michael })); LinkedHashSetString set1Clone1 = (LinkedHashSetString)set1.clone(); LinkedHashSetString set1Clone2 = (LinkedHashSetString)set1.clone(); LinkedHashSetString set2 = LinkedHashSetString set2 = Ryan }));new String[] { George , Katie , Kevin , Ryan })); set1.addAll(set2); set1Clone1.removeAll(set2); set1Clone2.retainAll(set2); The union of the two sets is The difference of the two sets is The union of the two sets is The difference of the two sets is The intersection of the two sets is + set1); + set1Clone1); + set1Clone2); System.out.println( System.out.println( } } 21.1 附加 import java.util.*; public class Exercise21_01Extra { public static void main(String[] args) { System.out.print( Enter a string: ); Scanner input = new Scanner(System.in); String s = input.nextLine(); Character[] list1 = { A , E , I , O , U }; SetCharacter vowels = new HashSet(Arrays.asList(list1)); SetCharacter vowelsInString = new HashSet(); SetCharacter consonantsInString = new HashSet(); int numbeOfVowels = 0; int numbeOfConsonants = 0; for ( int i = 0; i s.length(); i++) { char ch = Character.toUpperCase(s.charAt(i)); if (Character.isLetter(ch)) { if (vowels.contains(ch)) { if (!vowelsInString.contains(ch)) { vowelsInString.add(ch); numbeOfVowels++; } } else if (!consonantsInString.contains(ch)) { consonantsInString.add(ch); numbeOfConsonants++; } } } The number of vowels is The number of consonants is The number of vowels is The number of consonants is + numbeOfVowels); + numbeOfConsonants); System.out.println( } } 21.2 import java.util.*; import java.io.*; public class Exercise21_02 { public static void main(String[] args) { if (args.length != 1) { System.out.println( Usage: java Exercise21_02 fullfilename ); System.e

文档评论(0)

1亿VIP精品文档

相关文档