《Java语言程序设计与数据结构》编程练习答案(第二十章)(一)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
20.1
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<>();
for(int i=0;i<10;i++){
String tmp = input.nextLine();
strings.add(tmp);
}
Comparator<String> c = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
};
strings.sort(c);
for(int i=0;i<10;i++)
System.out.print(strings.get(i)+" ");
}
}
20.2
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList<Integer> test = new LinkedList<>();
System.out.println("Please enter numbers");
while(true){
int tmp = input.nextInt();
if(tmp == 0){
break;
}else{
if(!test.contains(tmp)) {
test.add(tmp);
}
}
}
System.out.println("The input is finished");
Comparator<Integer> com = (o1, o2) -> o1.compareTo(o2);
boolean loop = true;
while(loop){
String tmp = input.nextLine();
switch (tmp){
case "sort":
test.sort(com);
for(Integer i : test){
System.out.print(i+" ");
}
break;
case "reverse":
LinkedList<Integer> newList = new LinkedList<>();
while (!test.isEmpty()){
newList.addLast(test.removeLast());
}
test = newList;
for (Integer i : test){
System.out.print(i+" ");
}
break;
case "shuffle":
Collections.shuffle(test);
for (Integer i : test){
System.out.print(i+" ");
}
break;
case "break":
loop = false;
break;
default:
break;
}
}
}
}
20.3
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<question> test = new ArrayList<>();
test.add(new question("Liaoning","Shenyang"));
test.add(new question("Jilin","Changchun"));
while(true){
question tmp = test.get((int)(test.size()*Math.random()));
System.out.println(tmp.makeQuestion());
String ans = input.nextLine();
if(tmp.checkAnswer(ans)){
System.out.println("You are right");
}
else{
break;
}
}
}
}
class question{
private String province;
private String capital;
public question(String p, String c){
this.province = p;
this.capital = c;
}
public String makeQuestion(){
return "What is the capital of "+province;
}
public boolean checkAnswer(String ans){
return ans.equals(this.capital);
}
}
20.4
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Comparator<point> comparator = new Comparator<point>() {
@Override
public int compare(point o1, point o2) {
if(o1.getY() != o2.getY()){
return o1.getY()-o2.getY();
}else{
return o1.getX()-o2.getX();
}
}
};
point[] test = new point[100];
for(int i=0;i<100;i++){
test[i] = new point((int)(100*Math.random()),(int)(100*Math.random()));
}
Arrays.sort(test, comparator);
for(int i=0;i<5;i++){
System.out.println(test[i].toString());
}
}
}
class point{
private int x;
private int y;
public point(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public String toString(){
return "("+x+","+y+")";
}
}
20.5
🐴
20.6
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList<Integer> test = new LinkedList<>();
for(int i=0;i<5000000;i++){
test.addLast(i);
}
System.out.println("iterator");
long start = System.currentTimeMillis();
Iterator<Integer> iterator = test.iterator();
while(iterator.hasNext()){
iterator.next();
}
long end = System.currentTimeMillis();
System.out.println("Iterator costs "+(end - start)+" ms");
System.out.println("forEach");
start = System.currentTimeMillis();
test.forEach(e -> {e += 1;});
end = System.currentTimeMillis();
System.out.println("ForEach costs "+(end-start)+" ms");
}
}
20.7
🐎
20.8
🦓