@thatmaniscool

Как понять принцип работы кода из книги «Философия Джавы»?

Автор для пример дает вот такой код
import typeinfo.pets.*;
import java.util.*;
import static net.mindview.util.Print.*;
public class ListFeatures {
 public static void main(String[] args) {
 Random rand = new Random(47);
 List<Pet> pets = Pets.arrayList(7);
 print("1: " + pets);
 Hamster h = new Hamster();
 pets.add(h); // Automatically resizes
 print("2: " + pets);
 print("3: " + pets.contains(h));
 pets.remove(h); // Remove by object
 Pet p = pets.get(2);
 print("4: " + p + " " + pets.indexOf(p));
 Pet cymric = new Cymric();
 print("5: " + pets.indexOf(cymric));
 print("6: " + pets.remove(cymric));
 // Must be the exact object:
 print("7: " + pets.remove(p));
 print("8: " + pets);
 pets.add(3, new Mouse()); // Insert at an index
Holding Your Objects 283
 print("9: " + pets);
 List<Pet> sub = pets.subList(1, 4);
 print("subList: " + sub);
 print("10: " + pets.containsAll(sub));
 Collections.sort(sub); // In-place sort
 print("sorted subList: " + sub);
 // Order is not important in containsAll():
 print("11: " + pets.containsAll(sub));
 Collections.shuffle(sub, rand); // Mix it up
 print("shuffled subList: " + sub);
 print("12: " + pets.containsAll(sub));
 List<Pet> copy = new ArrayList<Pet>(pets);
 sub = Arrays.asList(pets.get(1), pets.get(4));
 print("sub: " + sub);
 copy.retainAll(sub);
 print("13: " + copy);
 copy = new ArrayList<Pet>(pets); // Get a fresh copy
 copy.remove(2); // Remove by index
 print("14: " + copy);
 copy.removeAll(sub); // Only removes exact objects
 print("15: " + copy);
 copy.set(1, new Mouse()); // Replace an element
 print("16: " + copy);
 copy.addAll(2, sub); // Insert a list in the middle
 print("17: " + copy);
 print("18: " + pets.isEmpty());
 pets.clear(); // Remove all elements
 print("19: " + pets);
 print("20: " + pets.isEmpty());
 pets.addAll(Pets.arrayList(4));
 print("21: " + pets);
 Object[] o = pets.toArray();
 print("22: " + o[3]);
 Pet[] pa = pets.toArray(new Pet[0]);
 print("23: " + pa[3].id());
 }
} /* Output:
1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]
2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]
3: true
4: Cymric 2
5: -1
6: false
7: true
8: [Rat, Manx, Mutt, Pug, Cymric, Pug]
9: [Rat, Manx, Mutt, Mouse, Pug, Cymric, Pug]
subList: [Manx, Mutt, Mouse]
10: true
sorted subList: [Manx, Mouse, Mutt]
11: true
shuffled subList: [Mouse, Manx, Mutt]
12: true
sub: [Mouse, Pug]
13: [Mouse, Pug]
14: [Rat, Mouse, Mutt, Pug, Cymric, Pug]
15: [Rat, Mutt, Cymric, Pug]
16: [Rat, Mouse, Cymric, Pug]
17: [Rat, Mouse, Mouse, Pug, Cymric, Pug]
18: false
19: []
20: true
21: [Manx, Cymric, Rat, EgyptianMau]
22: EgyptianMau

Но проблема заключается в том, что у меня нет импорт. класса typeinfo.pets.*;
Сам автор пишет на этот счет следующее

The following example reaches forward in the book to use a library from the Type
Information chapter by importing typeinfo.pets.

Но, у меня еще не было главы Type Information. Автор на полном серьезе предлагает пропустить часть книги или что?
  • Вопрос задан
  • 1223 просмотра
Пригласить эксперта
Ответы на вопрос 1
TrueBers
@TrueBers
Гуглю за еду
Вы руками что ли исходники из книги набиваете?
Вот же официальные исходники от автора. Скачайте и разбирайтесь себе спокойно.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
26 апр. 2024, в 07:47
2000 руб./за проект
26 апр. 2024, в 06:46
1000 руб./в час
26 апр. 2024, в 05:31
1000 руб./за проект