java中二维数组的定义和初始化。
java得到数组的长度。
抽象类与接口的比较。相同点,不同点。
List接口在Collections的基础上增加了大量的方法。代码示例:
import typeinfo.pets.*;
import java.util.*;
//import static net.mindview.util.println.*;
public class ListFeatures {
public static void main(String[] args) {
Random rand = new Random(47);
List<Pet> pets = Pets.arrayList(7);
System.out.println("1: " + pets);
Hamster h = new Hamster();
pets.add(h); // Automatically resizes
System.out.println("2: " + pets);
System.out.println("3: " + pets.contains(h));
pets.remove(h); // Remove by object
Pet p = pets.get(2);
System.out.println("4: " + p + " " + pets.indexOf(p));
Pet cymric = new Cymric();
System.out.println("5: " + pets.indexOf(cymric));
System.out.println("6: " + pets.remove(cymric));
// Must be the exact object:
System.out.println("7: " + pets.remove(p));
System.out.println("8: " + pets);
pets.add(3, new Mouse()); // Insert at an index
System.out.println("9: " + pets);
List<Pet> sub = pets.subList(1, 4);
System.out.println("subList: " + sub);
System.out.println("10: " + pets.containsAll(sub));
Collections.sort(sub); // 按规律排序 In-place sort
System.out.println("sorted subList: " + sub);
// Order is not important in containsAll():
System.out.println("11: " + pets.containsAll(sub));
Collections.shuffle(sub, rand); // Mix it up 打乱列表,生成乱序
System.out.println("shuffled subList: " + sub);
System.out.println("12: " + pets.containsAll(sub));
List<Pet> copy = new ArrayList<Pet>(pets);
sub = Arrays.asList(pets.get(1), pets.get(4));
System.out.println("sub: " + sub);
copy.retainAll(sub); //保留交集
System.out.println("13: " + copy);
copy = new ArrayList<Pet>(pets); // Get a fresh copy
copy.remove(2); // Remove by index
System.out.println("14: " + copy);
copy.removeAll(sub); // Only removes exact objects
System.out.println("15: " + copy);
copy.set(1, new Mouse()); // Replace an element
System.out.println("16: " + copy);
copy.addAll(2, sub); // Insert a list in the middle
System.out.println("17: " + copy);
System.out.println("18: " + pets.isEmpty());
pets.clear(); // Remove all elements
System.out.println("19: " + pets);
System.out.println("20: " + pets.isEmpty());
pets.addAll(Pets.arrayList(4));
System.out.println("21: " + pets);
Object[] o = pets.toArray();
System.out.println("22: " + o[3]);
Pet[] pa = pets.toArray(new Pet[0]);
System.out.println("23: " + pa[3].id());
}
}
ArrayList与List的区别:
List是一个接口,而ArrayList则是对于接口List的实现。
为什么要用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢?
问题就在于List接口有多个实现类,现在你用的是ArrayList,也许哪一天你需要换成其它的实现类,如 LinkedList或者Vector等等,这时你只要改变这一行就行了: List list = new LinkedList(); 其它使用了list地方的代码根本不需要改动。
假设你开始用ArrayList alist = new ArrayList(), 这下你有的改了,特别是如果你使用了ArrayList实现类特有的方法
**所以,java集合框架的学习十分重要。**
**迭代器**将遍历序列的操作和序列底层的结构分离。迭代器统一了对容器的访问方式。迭代器也是一种 **设计模式。**
import typeinfo.pets.*;
import java.util.*;
public class CrossContainerIteration {
public static void display(Iterator<Pet> it) {
while(it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
}
public static void main(String[] args) {
ArrayList<Pet> pets = Pets.arrayList(8);
LinkedList<Pet> petsLL = new LinkedList<Pet>(pets);
HashSet<Pet> petsHS = new HashSet<Pet>(pets);
TreeSet<Pet> petsTS = new TreeSet<Pet>(pets);
display(pets.iterator());
display(petsLL.iterator());
display(petsHS.iterator());
display(petsTS.iterator());
}
} /* Output:
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
4:Pug 6:Pug 3:Mutt 1:Manx 5:Cymric 7:Manx 2:Cymric 0:Rat
5:Cymric 2:Cymric 7:Manx 1:Manx 3:Mutt 6:Pug 4:Pug 0:Rat
实现举例2:
class Gerbil {
private int gerbilNumber;
public Gerbil(int i) {
gerbilNumber = i;
}
public void hop() {
System.out.println("Gerbil " + gerbilNumber + " hops");
}
}
public class Ex8 {
public static void main(String []args) {
ArrayList<Gerbil> a = new ArrayList<Gerbil>();
for(int i=0;i<10;i++)
{
a.add(new Gerbil(i));
}
Iterator<Gerbil> b = a.iterator();
while(b.hasNext())
{
b.next().hop();
}
}
}
实现举例3:
public class Ex11 {
public static void Inter(Collection a)
{
Iterator it = a.iterator();
while(it.hasNext())
{
System.out.print(it.next()+" ");
}
System.out.println();
}
public static void main(String[] args)
{
ArrayList<Integer> a = new ArrayList<Integer>(Arrays.asList(1,2,3));
ArrayList<Character> b = new ArrayList<Character>(Arrays.asList('a','b','c'));
Inter(a);
Inter(b);
System.out.print(Arrays.asList(1,2,3));
}
}
LinkedList也像ArrayList一样实现了基本的List接口。它的插入和移除更加高效,而在随机访问方面要逊色一些。而 **Queue接口**在LinkedList的基础上添加了element(),offer(),peek(),poll(),remove()等方法。
**Queue接口**在并发编程中特别重要,它们可以安全地将对象从一个任务传递给另外一个任务。 linkedList提供了方法以支持队列的行为,并且它实现了Queue接口。
**Collection接口和Iterator都可以将display()方法和底层容器的特定实现解耦。**
import typeinfo.pets.*;
import java.util.*;
public class InterfaceVsIterator {
public static void display(Iterator<Pet> it) {
while(it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
}
public static void display(Collection<Pet> pets) {
for(Pet p : pets)
System.out.print(p.id() + ":" + p + " ");
System.out.println();
}
public static void main(String[] args) {
List<Pet> petList = Pets.arrayList(8);
Set<Pet> petSet = new HashSet<Pet>(petList);
Map<String,Pet> petMap =
new LinkedHashMap<String,Pet>();
String[] names = ("Ralph, Eric, Robin, Lacey, " +
"Britney, Sam, Spot, Fluffy").split(", ");
for(int i = 0; i < names.length; i++)
petMap.put(names[i], petList.get(i));
display(petList);
display(petSet);
display(petList.iterator());
display(petSet.iterator());
System.out.println(petMap);
System.out.println(petMap.keySet());
display(petMap.values());
display(petMap.values().iterator());
}
}
**总结**
java提供了大量持有对象的方式。
点线框表示接口,实线框表示普通的类,空心箭头表示某个类可以生成箭头所指向的类的对象。
! [](D:\markdown_picture\java简单的容器分类.png)
java得到数组的长度。
抽象类与接口的比较。相同点,不同点。
List接口在Collections的基础上增加了大量的方法。代码示例:
import typeinfo.pets.*;
import java.util.*;
//import static net.mindview.util.println.*;
public class ListFeatures {
public static void main(String[] args) {
Random rand = new Random(47);
List<Pet> pets = Pets.arrayList(7);
System.out.println("1: " + pets);
Hamster h = new Hamster();
pets.add(h); // Automatically resizes
System.out.println("2: " + pets);
System.out.println("3: " + pets.contains(h));
pets.remove(h); // Remove by object
Pet p = pets.get(2);
System.out.println("4: " + p + " " + pets.indexOf(p));
Pet cymric = new Cymric();
System.out.println("5: " + pets.indexOf(cymric));
System.out.println("6: " + pets.remove(cymric));
// Must be the exact object:
System.out.println("7: " + pets.remove(p));
System.out.println("8: " + pets);
pets.add(3, new Mouse()); // Insert at an index
System.out.println("9: " + pets);
List<Pet> sub = pets.subList(1, 4);
System.out.println("subList: " + sub);
System.out.println("10: " + pets.containsAll(sub));
Collections.sort(sub); // 按规律排序 In-place sort
System.out.println("sorted subList: " + sub);
// Order is not important in containsAll():
System.out.println("11: " + pets.containsAll(sub));
Collections.shuffle(sub, rand); // Mix it up 打乱列表,生成乱序
System.out.println("shuffled subList: " + sub);
System.out.println("12: " + pets.containsAll(sub));
List<Pet> copy = new ArrayList<Pet>(pets);
sub = Arrays.asList(pets.get(1), pets.get(4));
System.out.println("sub: " + sub);
copy.retainAll(sub); //保留交集
System.out.println("13: " + copy);
copy = new ArrayList<Pet>(pets); // Get a fresh copy
copy.remove(2); // Remove by index
System.out.println("14: " + copy);
copy.removeAll(sub); // Only removes exact objects
System.out.println("15: " + copy);
copy.set(1, new Mouse()); // Replace an element
System.out.println("16: " + copy);
copy.addAll(2, sub); // Insert a list in the middle
System.out.println("17: " + copy);
System.out.println("18: " + pets.isEmpty());
pets.clear(); // Remove all elements
System.out.println("19: " + pets);
System.out.println("20: " + pets.isEmpty());
pets.addAll(Pets.arrayList(4));
System.out.println("21: " + pets);
Object[] o = pets.toArray();
System.out.println("22: " + o[3]);
Pet[] pa = pets.toArray(new Pet[0]);
System.out.println("23: " + pa[3].id());
}
}
ArrayList与List的区别:
List是一个接口,而ArrayList则是对于接口List的实现。
为什么要用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢?
问题就在于List接口有多个实现类,现在你用的是ArrayList,也许哪一天你需要换成其它的实现类,如 LinkedList或者Vector等等,这时你只要改变这一行就行了: List list = new LinkedList(); 其它使用了list地方的代码根本不需要改动。
假设你开始用ArrayList alist = new ArrayList(), 这下你有的改了,特别是如果你使用了ArrayList实现类特有的方法
**所以,java集合框架的学习十分重要。**
**迭代器**将遍历序列的操作和序列底层的结构分离。迭代器统一了对容器的访问方式。迭代器也是一种 **设计模式。**
import typeinfo.pets.*;
import java.util.*;
public class CrossContainerIteration {
public static void display(Iterator<Pet> it) {
while(it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
}
public static void main(String[] args) {
ArrayList<Pet> pets = Pets.arrayList(8);
LinkedList<Pet> petsLL = new LinkedList<Pet>(pets);
HashSet<Pet> petsHS = new HashSet<Pet>(pets);
TreeSet<Pet> petsTS = new TreeSet<Pet>(pets);
display(pets.iterator());
display(petsLL.iterator());
display(petsHS.iterator());
display(petsTS.iterator());
}
} /* Output:
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
4:Pug 6:Pug 3:Mutt 1:Manx 5:Cymric 7:Manx 2:Cymric 0:Rat
5:Cymric 2:Cymric 7:Manx 1:Manx 3:Mutt 6:Pug 4:Pug 0:Rat
实现举例2:
class Gerbil {
private int gerbilNumber;
public Gerbil(int i) {
gerbilNumber = i;
}
public void hop() {
System.out.println("Gerbil " + gerbilNumber + " hops");
}
}
public class Ex8 {
public static void main(String []args) {
ArrayList<Gerbil> a = new ArrayList<Gerbil>();
for(int i=0;i<10;i++)
{
a.add(new Gerbil(i));
}
Iterator<Gerbil> b = a.iterator();
while(b.hasNext())
{
b.next().hop();
}
}
}
实现举例3:
public class Ex11 {
public static void Inter(Collection a)
{
Iterator it = a.iterator();
while(it.hasNext())
{
System.out.print(it.next()+" ");
}
System.out.println();
}
public static void main(String[] args)
{
ArrayList<Integer> a = new ArrayList<Integer>(Arrays.asList(1,2,3));
ArrayList<Character> b = new ArrayList<Character>(Arrays.asList('a','b','c'));
Inter(a);
Inter(b);
System.out.print(Arrays.asList(1,2,3));
}
}
LinkedList也像ArrayList一样实现了基本的List接口。它的插入和移除更加高效,而在随机访问方面要逊色一些。而 **Queue接口**在LinkedList的基础上添加了element(),offer(),peek(),poll(),remove()等方法。
**Queue接口**在并发编程中特别重要,它们可以安全地将对象从一个任务传递给另外一个任务。 linkedList提供了方法以支持队列的行为,并且它实现了Queue接口。
**Collection接口和Iterator都可以将display()方法和底层容器的特定实现解耦。**
import typeinfo.pets.*;
import java.util.*;
public class InterfaceVsIterator {
public static void display(Iterator<Pet> it) {
while(it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
}
public static void display(Collection<Pet> pets) {
for(Pet p : pets)
System.out.print(p.id() + ":" + p + " ");
System.out.println();
}
public static void main(String[] args) {
List<Pet> petList = Pets.arrayList(8);
Set<Pet> petSet = new HashSet<Pet>(petList);
Map<String,Pet> petMap =
new LinkedHashMap<String,Pet>();
String[] names = ("Ralph, Eric, Robin, Lacey, " +
"Britney, Sam, Spot, Fluffy").split(", ");
for(int i = 0; i < names.length; i++)
petMap.put(names[i], petList.get(i));
display(petList);
display(petSet);
display(petList.iterator());
display(petSet.iterator());
System.out.println(petMap);
System.out.println(petMap.keySet());
display(petMap.values());
display(petMap.values().iterator());
}
}
**总结**
java提供了大量持有对象的方式。
点线框表示接口,实线框表示普通的类,空心箭头表示某个类可以生成箭头所指向的类的对象。
! [](D:\markdown_picture\java简单的容器分类.png)