云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 《Java语言程序设计(基础篇)》(第10版 梁勇 著)第二十一章练习题答案

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

  • 62 次阅读
  • 3 次下载
  • 2025/5/6 8:02:55

《Java语言程序设计(基础篇)》(第10版 梁勇 著)

第二十一章 练习题答案

21.1

import java.util.*;

public class Exercise21_01 {

public static void main(String[] args) {

LinkedHashSet set1 = new LinkedHashSet(Arrays.asList( new String[]{\, \, \, \, \, \}));

LinkedHashSet set1Clone1 = (LinkedHashSet)set1.clone(); LinkedHashSet set1Clone2 = (LinkedHashSet)set1.clone();

LinkedHashSet set2 = new LinkedHashSet(Arrays.asList( new String[] {\, \, \, \, \}));

set1.addAll(set2);

set1Clone1.removeAll(set2); set1Clone2.retainAll(set2);

System.out.println(\ + set1);

System.out.println(\ + set1Clone1); System.out.println(\ + set1Clone2); } }

21.1附加

import java.util.*;

public class Exercise21_01Extra {

public static void main(String[] args) { System.out.print(\); Scanner input = new Scanner(System.in); String s = input.nextLine();

Character[] list1 = {'A', 'E', 'I', 'O', 'U'};

Set vowels = new HashSet<>(Arrays.asList(list1)); Set vowelsInString = new HashSet<>(); Set 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++; } } }

System.out.println(\ + numbeOfVowels); System.out.println(\ + numbeOfConsonants); } }

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(

\); System.exit(1); }

String filename = args[0];

// Create a tree set to hold the words

TreeSet treeSet = new TreeSet();

try {

Scanner in = new Scanner(new File(filename));

String line;

while ((line = in.nextLine()) != null) {

String[] tokens = line.split(\);

for (int i = 0; i < tokens.length; i++) treeSet.add(tokens[i]); } }

catch (Exception ex) { System.err.println(ex); }

// Get an iterator for the set

Iterator iterator = treeSet.iterator();

// Display mappings

System.out.println(\); while (iterator.hasNext()) {

System.out.println(iterator.next()); } } }

21.3

import java.util.*; import java.io.*;

public class Exercise21_03 {

public static void main(String[] args) { // Check usage

if (args.length != 1) {

System.out.println(\); System.exit(1); }

// Array of all Java keywords + true + null

String[] keywordString = {\, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \,

\, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \};

Set keywordSet =

new HashSet(Arrays.asList(keywordString)); int count = 0; try {

Scanner input = new Scanner(new File(args[0]));

String text = \;

while (input.hasNext()) {

String line = input.nextLine(); line = stripLineComments(line); line = stripLineStringLiterals(line); text += line + \; }

text = stripParagraghComments(text);

String[] tokens = text.split(\); for (String token: tokens) { if (keywordSet.contains(token)) count++; }

System.out.println(\ + count); }

catch (Exception ex) { ex.printStackTrace(); } }

/* Strip line comments */

private static String stripLineComments(String line) { int index = line.indexOf(\);

if (index < 0) return line; else

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

《Java语言程序设计(基础篇)》(第10版 梁勇 著) 第二十一章 练习题答案 21.1 import java.util.*; public class Exercise21_01 { public static void main(String[] args) { LinkedHashSet set1 = new LinkedHashSet(Arrays.asList( new String[]{\, \, \, \, \, \})); LinkedHashSet set1Clone1 = (LinkedHashSet)set1.clone(); Linked

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com