- 浏览: 138861 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
DRUNKonSTREET:
66666
Thinking in java 的课后习题答案以及源代码下载 -
疯青春you:
net.mindview.util.Print.*; 这个包在 ...
Thinking in java 的课后习题答案以及源代码下载 -
hpf_888:
想问一下,注解的方式下,如何能实现动态sql效果?
MyBatis动态SQL -
Spirit_eye:
楼主好人一生平安
Thinking in java 的课后习题答案以及源代码下载 -
lycrystal818:
最近发现基础知识不太扎实,买了本think in java 看 ...
Thinking in java 的课后习题答案以及源代码下载
第11章 持有对象
练习1:
/** * 创建一个新类Gerbil(沙鼠),包含int gerbilNumber, * 在构造器中初始化它。添加一个方法hop(),用以打印沙鼠的号码以及他 * 正在跳跃的信息。创建一个ArrayList,并向其中添加一串Gerbil对象。 * 使用get()遍历List,并且对每个Gerbil调用hop() */ import java.util.ArrayList; public class Gerbil { private static int counter; private int gerbilNumber; private String hopInfo; public Gerbil(){} public Gerbil(String hl){ gerbilNumber = ++counter; hopInfo = hl; } void hop(){ System.out.println("沙鼠的号码为:" + gerbilNumber + "跳跃的信息为:" + hopInfo); } public static void main(String[] args) { ArrayList<Gerbil> gerbils = new ArrayList<Gerbil>(); gerbils.add(new Gerbil("25")); gerbils.add(new Gerbil("26")); gerbils.add(new Gerbil("24")); gerbils.add(new Gerbil("25")); for(Gerbil g: gerbils){ g.hop(); } } }
练习2:
/** * 修改SimpleCollection.java, * 使用Set来表示c。 */ import java.util.HashSet; import java.util.Set; public class Exam_11_2 { public static void main(String[] args) { Set<Integer> c = new HashSet<Integer>(); for(int i = 0;i <10;i++) c.add(i); for(Integer i : c) System.out.print(i + ", "); } }
练习4
//创建一个生成器类,它可以在每次调用其next()方法时,产生你由你最喜欢的电影 //(你可以使用Snow White或Star Wars)的字符构成的名字(作为String对象)。对字符列表中的电影名 //用完之后,循环到这个字符列表的开始处。使用这个生成器来填充数组、ArrayList、LinkedList、HashSet //LinkedHashSet和TreeSet,然后打印每一个容器。 import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.TreeSet; class Generator { int key = 0; public String next() { switch (key) { case 0: key++; return "Snow White"; case 1: key++; return "Bashful"; case 2: key++; return "Doc"; case 3: key++; return "Dopey"; case 4: key++; return "Grumpy"; case 5: key++; return "Happy"; case 6: key++; return "Sleepy"; default: case 7: key++; return "Sneezy"; } } public void fillA(String[] a) { for (int i = 0; i < a.length; i++) { a[i] = next(); } } public Collection fill(Collection<String> c, int n) { for (int i = 0; i < n; i++) c.add(next()); return c; } } public class Ex4 { public static void main(String[] args) { Generator gen = new Generator(); String[] a = new String[10]; gen.fillA(a); for(String s : a)System.out.println(s + ","); System.out.println(); System.out.println(gen.fill(new ArrayList<String>(), 10)); System.out.println(gen.fill(new LinkedList<String>(), 10)); System.out.println(gen.fill(new HashSet<String>(),10)); System.out.println(gen.fill(new LinkedHashSet<String>(),10)); System.out.println(gen.fill(new TreeSet<String>(),10)); } }
练习5
//修改ListFeatures.java,让它使用Integer(记住自动包装机制!)而不是Pet,并解释在结果上有何不同。 import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; public class Ex5 { public static List<Integer> listofRandInteger(int length, int n) { Random rand = new Random(); List<Integer> li = new ArrayList<Integer>(); for(int i = 0; i < length; i++) li.add(rand.nextInt(n)); return li; } public static void main(String[] args) { Random rand = new Random(); List<Integer> li = listofRandInteger(7,10); System.out.println("1: " + li); Integer h= new Integer(rand.nextInt(10)); li.add(h); System.out.println("2: " + li); System.out.println("3: " + li.contains(h)); //removes the first instance equivalent to Integer h: li.remove(h); System.out.println("3.5 " + li); Integer p = li.get(2); System.out.println("4: " + p + " " + li.indexOf(p)); Integer cy = new Integer(rand.nextInt(10)); System.out.println("5: " + cy + " " + li.indexOf(cy)); System.out.println("6: " + li.remove(cy)); System.out.println("7: " + li.remove(p)); System.out.println("8: " + li); li.add(3, new Integer(rand.nextInt(10))); System.out.println("9: " + li); List<Integer> sub = li.subList(1, 4); System.out.println("sublist: " + sub); System.out.println("10: " + li.containsAll(sub)); Collections.sort(sub); System.out.println("sorted sublist: " + sub); System.out.println("11: " + li.containsAll(sub)); System.out.println("11.25: " + li); Collections.shuffle(sub,rand); System.out.println("11.5: " + li); System.out.println("shuffled sublist: " + sub); System.out.println("12: " + li.containsAll(sub)); List<Integer> copy = new ArrayList<Integer>(li); System.out.println("12.5 " + li); sub = Arrays.asList(li.get(1),li.get(4)); System.out.println("sub: " + sub); copy.retainAll(sub); System.out.println("13: " + copy); copy = new ArrayList<Integer>(li); copy.remove(2); System.out.println("14: " +copy); copy.removeAll(sub); System.out.println("15: " +copy); if(copy.size() > 1) copy.set(1,8); System.out.println("16: " +copy); if(copy.size() > 2) copy.addAll(2, sub); System.out.println("17: " + copy); System.out.println("18: " + li.isEmpty()); li.clear(); System.out.println("19: " + li); System.out.println("20: " + li.isEmpty()); li.addAll(listofRandInteger(4,10)); System.out.println("21: " + li); Object[] o = li.toArray(); System.out.println("22: " + o[3]); Integer[] ia = li.toArray(new Integer[0]); System.out.println("23: " + ia[3]); } }
练习8
import java.util.ArrayList; import java.util.Iterator; //修改练习1,以便调用hop()时使用Iterator遍历List class Gerbil2 { private static int counter; private int gerbilNumber; public Gerbil2(){ gerbilNumber = ++counter; } public Gerbil2(String hl){ gerbilNumber = ++counter; } void hop(){ System.out.println("沙鼠的号码为:" + gerbilNumber ); } } public class Ex8 { public static void main(String[] args) { ArrayList<Gerbil2> gerbils = new ArrayList<Gerbil2>(); for(int i=0;i<10;i++) { gerbils.add(new Gerbil2()); } Iterator<Gerbil2> it = gerbils.iterator(); while(it.hasNext()) it.next().hop(); } }
练习11
//写一个方法,使用Iterator遍历Collection,并打印容器中每个对象的toString(). //填充各种类型的Collection然后对其使用此方法 import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.TreeSet; public class Ex11 { public static void printAny(Collection c){ Iterator it = c.iterator(); while(it.hasNext()) System.out.print(it.next()+ " "); System.out.println(); } public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(Arrays.asList(1,2,3)); LinkedList<Character> ll = new LinkedList<Character>(Arrays.asList('a','b','c')); HashSet<Float> hs = new HashSet<Float>(Arrays.asList(1.1f,2.2f,3.3f)); TreeSet<Double> ts = new TreeSet<Double>(Arrays.asList(1.11,2.22,3.33)); LinkedHashSet<Integer> lhs = new LinkedHashSet<Integer>(Arrays.asList(11,22,33)); printAny(al); printAny(ll); printAny(hs); printAny(ts); printAny(lhs); } }
练习12
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.ListIterator; //创建并组装一个List<Integer>,然后创建第二个具有相同尺寸的List<Integer>,并使用ListIterator读取 //第一个List中的元素,然后再将它们以反序插入到第二个列表中(你可能想探索该问题的各种解决之道) public class Ex12 { public static void main(String[] args) { List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4)); List<Integer> list2 = new ArrayList<Integer>(Arrays.asList(5,6,7,8,9)); ListIterator<Integer> it1 = list1.listIterator(); ListIterator<Integer> it2 = list2.listIterator(); for(Integer x : list2) System.out.print(x+" "); System.out.println(); while(it1.hasNext()){ it1.next(); } while(it2.hasNext()){ it2.next(); it2.set(it1.previous()); } for(Integer x : list2) System.out.print(x+" "); } }
练习14
import java.util.LinkedList; import java.util.ListIterator; //创建一个空的LinkedList<Integer>,通过使用ListIterator,将若干个Integer插入这个List中,插入时, //总是将它们插入到List的中间 public class Ex14 { static void addMiddle(LinkedList<Integer> l,Integer[] ia){ for(Integer i : ia){ ListIterator<Integer> it = l.listIterator((l.size()/2)); it.add(i); System.out.println(l); } } public static void main(String[] args) { LinkedList<Integer> li = new LinkedList<Integer>(); Integer[] x = {0,1,2,3, 4, 5, 6, 7}; Ex14.addMiddle(li, x); } }
练习17
/*使用练习1中的Gerbill类,将其放入Map中,将每个Gerbil的名字(例如Fuzzy或Spot)String(键) 与每个Gerbil(值)关联起来。为KeySet()获取Iterator,使用它遍历Map,针对每个"键"查询 Gerbil,然后打印出 "键",并让gerbil执行hop()*/ package com.evelen; import java.util.HashMap; import java.util.Iterator; import java.util.Map; class Gerbil3 { private int gerbilNumber; public Gerbil3(int i){ gerbilNumber = i; } public void hop() { System.out.println("gerbil " + gerbilNumber + " hops"); } } public class Gerbils17 { public static void main(String[] args) { Map<String,Gerbil3> gerbils = new HashMap<String,Gerbil3>(); gerbils.put("Fuzzy", new Gerbil3(0)); gerbils.put("Spot", new Gerbil3(1)); gerbils.put("Speedy", new Gerbil3(2)); gerbils.put("Dopey", new Gerbil3(3)); gerbils.put("Sleepy", new Gerbil3(4)); gerbils.put("Happy", new Gerbil3(5)); Iterator<String> it = gerbils.keySet().iterator(); while(it.hasNext()) { String s = it.next(); System.out.print(s + ": "); gerbils.get(s).hop(); } } }
练习18
package com.evelen; // holding/Ex18.java // TIJ4 Chapter Holding, Exercise 18, page 422 /* Fill a HashMap with key-value pairs. Print the results to show ordering * by hash code. Extract the pairs, sort by key, and place the result into a * LinkedHashMap. Show that the insertion order is maintained. */ import java.util.*; public class Ex18 { public static void main(String[] args) { Map<String, Gerbil> gerbils = new HashMap<String, Gerbil>(); gerbils.put("Fuzzy", new Gerbil(0)); gerbils.put("Spot", new Gerbil(1)); gerbils.put("Speedy", new Gerbil(2)); gerbils.put("Dopey", new Gerbil(3)); gerbils.put("Sleepy", new Gerbil(4)); gerbils.put("Happy", new Gerbil(5)); gerbils.put("Funny", new Gerbil(6)); gerbils.put("Silly", new Gerbil(7)); gerbils.put("Goofy", new Gerbil(8)); gerbils.put("Wowee", new Gerbil(9)); System.out.println("1"+gerbils); System.out.println(); Set<String> sortedKeys = new TreeSet<String>(gerbils.keySet()); System.out.println("2"+sortedKeys); System.out.println(); Map<String, Gerbil> sortedGerbils = new LinkedHashMap<String, Gerbil>(); for(String s : sortedKeys) { System.out.print("Adding " + s + ", "); sortedGerbils.put(s, gerbils.get(s)); } System.out.println(); System.out.println(); System.out.println("3"+sortedGerbils); System.out.println(); // or, just: Map<String, Gerbil> sortedGerbils2 = new TreeMap<String, Gerbil>(gerbils); System.out.println("4"+sortedGerbils2); } }
练习27
package com.evelen; // holding/Queue27.java // TIJ4 Chapter Holding, Exercise 27, page 424 /* Write a class called Command that contains a String and has a method operation() * that displays the String. Write a second class with a method that fills a Queue * with Command objects and returns it. Pass the filled Queue to a method in a third * class that consumes the objects in the Queue and calls their operation() methods. */ import java.util.*; class Command { String s; Command(String s) { this.s = s; } void operation() { System.out.print(s); } } class Build { Queue<Command> makeQ() { Queue<Command> q = new LinkedList<Command>(); for(int i = 0; i < 10; i++) q.offer(new Command(i + " ")); return q; } } public class Ex27 { public static void commandEater(Queue<Command> qc) { while(qc.peek() != null) qc.poll().operation(); } public static void main(String[] args) { Build b = new Build(); commandEater(b.makeQ()); } }
练习28
// holding/Ex28.java // TIJ4 Chapter Holding, Exercise 28, page 427 /* Fill a PriorityQueue (using offer()) with Double values created using * java.util.Random, then remove the elements using poll() and display them. */ import java.util.*; public class Ex28 { public static void main(String[] args) { Random rand = new Random(); PriorityQueue<Double> d = new PriorityQueue<Double>(); for(int i = 0; i < 10; i++) d.offer(rand.nextDouble() * i); while(d.peek() != null) System.out.print(d.poll() + " "); } }
练习29
package com.evelen; // holding/Ex29.java // TIJ4 Chapter Holding, Exercise 29, page 427 /* Fill a PriorityQueue (using offer()) with Double values created using * java.util.Random, then remove the elements using poll() and display them. */ import java.util.*; class Simple extends Object {} public class Ex29 { public static void main(String[] args) { PriorityQueue<Simple> s = new PriorityQueue<Simple>(); // OK to add one Simple: s.offer(new Simple()); // but no more allowed; get runtime exception: // Simple cannot be cast to Comparable: s.offer(new Simple()); } }
相关推荐
"Thinking in Java 习题答案"是配套的解答集,为读者提供了书中习题的解决方案,帮助读者更好地理解和应用所学知识。 习题答案通常涵盖以下几个关键知识点: 1. **基础语法**:包括变量声明、数据类型、运算符、...
这个压缩包提供的就是《Thinking in Java 4RD》的课后习题答案,以及可能包含的PDF版本教材,这对于自学或者教学来说都是极其宝贵的资源。 在《Thinking in Java》中,你可以学习到以下主要知识点: 1. **Java基础...
本书赢得了全球程序员的广泛赞誉,即使是最晦涩的概念,在Bruce Eckel...从Java的基础语法到最高级特性(深入的面向对象概念、多线程、自动项目构建、单元测试和调试等),本书都能逐步指导你轻松掌握。最新版本的哦
《Thinking In Java》第四版,和书本源码及其课后习题答案
该压缩包文件“Thinking In Java 练习题答案 第四版]Annotated+Solution+Guide+for+TIJ4.pdf”包含了《Thinking in Java》第四版的全部习题解答,这些解答通常会包括对问题的解析、代码实现以及可能的优化建议。...
4. "Thinking In Java 练习题答案第四版" - 这应该是第4版的习题答案,与第3版类似,提供了解答,帮助读者巩固所学。 书中的核心知识点可能包括但不限于以下内容: 1. **基础语法**:变量、数据类型、运算符、流程...
《Java思想与课后习题》是一本深受Java开发者喜爱的资源,主要基于 Bruce Eckel 的经典著作《Thinking in Java》。这本书深入浅出地介绍了Java编程语言的核心概念和技术,同时提供了丰富的课后习题来帮助读者巩固所...
"thinking in java 4课后习题答案"是针对该书第四版的习题解答,对读者在学习过程中遇到的问题提供了参考。 习题解答通常会按照书中的章节结构进行划分,每个章节的习题都有对应的解答,帮助读者理解并巩固所学知识...
本压缩包中的"Annotated+Solution+Guide+for+Thinking+in+Java+4th+Edition"文件,是针对第四版《Thinking in Java》的习题解答,包含了大量的代码示例。这个解答指南不仅提供了正确答案,还对每个问题进行了详尽的...
在阅读《Thinking in Java》时,课后的练习题是非常重要的部分。这些练习题设计巧妙,旨在检验和巩固读者对各个章节知识的理解,涵盖面向对象编程、类与对象、封装、继承、多态、接口、异常处理、集合框架、泛型、IO...
根据提供的文件信息,我们可以推断出这是一份关于《Thinking in Java》第四版一书的课后习题解答文档的相关版权说明与免责声明。下面将基于这些信息,详细解析该文件中涉及的重要知识点。 ### 关于《Thinking in ...
以上知识点只是《Thinking in Java》第四版课后习题可能涉及的一部分,每个主题都可能有深度和广度上的扩展,因此在解答习题时,不仅要理解答案,还要深入探究背后的原理,才能真正掌握Java这门强大的编程语言。
本压缩包包含的是"中文3课后答案",这意味着它提供了书中第三部分(通常涵盖高级主题)练习题的解答,这对于自学者和希望深化理解的读者来说是非常有价值的资源。 首先,我们要明白《Thinking in Java》第三部分...
Annotated Solution Guide for Thinking in Java 4th Edition中文资源名称:Thinking In Java第四版 课后练习答案。根据yunqi_2008上传的分章整理后上传,整理了近一个小时,若是觉得分数太高可以找yunqi_2008的下载...
Thinking in java4th +习题答案,亲测正版!手工把每一章分开。22章+附录+目录+前言+习题答案
"Java课后习题答案"这个压缩包提供的是针对Java编程基础篇的课后编程题解,对初学者来说是非常宝贵的资源。 Java的基础主要包括语法、数据类型、控制结构、类与对象、接口、异常处理、集合框架等核心概念。下面我们...
《Thinking in Java 4th Edition with Annotated Solution Guide》是一本经典的Java编程教材,由Bruce Eckel撰写。这本书深入浅出地介绍了Java编程的核心概念和技术,对于初学者和经验丰富的开发者来说,都是一个...
总的来说,这个压缩包提供的《Thinking in Java》第四版课后习题答案,对于正在学习或已经工作在Java领域的开发者来说,是提升技能、解决实际问题的宝贵资料。通过仔细研究和实践这些习题,你可以增强自己在并发编程...
"Thinking in Java 答案"则是对应教材的课后习题解答,帮助读者检验和巩固所学知识。 在学习Java的过程中,理解并解决《Thinking in Java》中的习题是至关重要的。这些题目涵盖了类与对象、封装、继承、多态、接口...
《Thinking in Java》是Bruce Eckel的经典之作,深入浅出地介绍了...通过解决这些习题,你将深入理解《Thinking in Java》第二章中的概念,并提升你的Java编程能力。不断实践和思考,是成为熟练Java开发者的必经之路。