集合
集合由来:数组操作数据的弊端
数组弊端:
1.只能添加相同类型的元素(基本数据类型和引用数据类型)
2.长度一旦确定,就不能改变
要添加超出数组长度个数的元素,操作复杂
集合的特点:
1.能添加不同类型的元素
注意:集合中只能添加引用数据类型(对象类型)
2.长度可变
集合框架体系
集合的基本方法
Collection collection = new ArrayList();
boolean add = collection.add("a");
boolean add2 = collection.add("b");
boolean add3 = collection.add("c");
boolean add4 = collection.add(10);
boolean add5 = collection.add(true);
System.out.println(collection.toString());
int size = collection.size();
System.out.println(size);
boolean contains = collection.contains(10);
System.out.println(contains);
boolean remove = collection.remove(true);
System.out.println(remove);
System.out.println(collection);
boolean empty = collection.isEmpty();
System.out.println(empty);
collection.clear();
System.out.println(collection);
打印字符串
private static void function() {
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
Object[] array = collection.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
创建一个集合,加入三个学生,从集合中取出学生姓名
private static void function() {
Collection collection = new ArrayList();
collection.add(new Student("学生1", 18));
collection.add(new Student("学生2", 18));
collection.add(new Student("学生3", 18));
而不是把保存对象的容器(数组)转化)
Object[] array = collection.toArray();
for (int i = 0; i < array.length; i++) {
Student student = (Student)array[i];
System.out.println(student.getName());
}
}
错误的强转方式
public static void function() {
Collection collection = new ArrayList();
collection.add(new Student("学生1", 18));
collection.add(new Student("学生2", 18));
collection.add(new Student("学生3", 18));
Student[] array = (Student[])collection.toArray();
for (Student student : array) {
System.out.println(student);
}
}
addAll
private static void function() {
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
Collection collection2 = new ArrayList();
collection2.add("x");
collection2.add("y");
collection2.add("z");
collection.addAll(collection2);
System.out.println("c:" + collection);
System.out.println("c2:" + collection2);
}
removeAll
private static void function() {
Collection collection = new ArrayList()
collection.add("a")
collection.add("b")
collection.add("c")
Collection collection2 = new ArrayList()
collection2.add("x")
collection2.add("y")
collection2.add("z")
// 集合调用方法,将该集合中两个集合相同的所有元素删除
collection.removeAll(collection2)
System.out.println("c:" + collection)
System.out.println("c2:" + collection2)
}
retainAll
private static void function() {
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
Collection collection2 = new ArrayList();
collection2.add("x");
collection2.add("y");
collection2.add("z");
collection.retainAll(collection2);
System.out.println("c:" + collection);
System.out.println("c2:" + collection2);
}
迭代器(遍历)
// 测试迭代器中的方法
Collection collection = new ArrayList()
collection.add("a")
collection.add("b")
collection.add("c")
// 获取集合中的迭代器
Iterator iterator = collection.iterator()
// 先判断集合中是否有元素
boolean hasNext = iterator.hasNext()
System.out.println(hasNext)
// 从集合中取出元素
Object next = iterator.next()
System.out.println(next)
遍历集合
private static void function() {
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
创建一个集合
保存3学生
使用迭代器遍历,打印学生姓名
private static void function() {
Collection collection = new ArrayList();
collection.add(new Student("学生1", 18));
collection.add(new Student("学生2", 18));
collection.add(new Student("学生3", 18));
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Student student = (Student) next;
System.out.println(student.getName());
}
}
综合引用–图书馆
public class Books {
private String bookName;
private String author;
public Books() {
super();
}
public Books(String bookName, String author) {
super();
this.bookName = bookName;
this.author = author;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "[书名:" + bookName + ", 作者:" + author + "]";
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
@SuppressWarnings({"rawtypes", "unused","unchecked"})
public class Library {
private String libraryName;
private Collection books;
public Library() {
super();
this.books = new ArrayList();
}
public Library(String libraryName) {
super();
this.libraryName = libraryName;
this.books = new ArrayList();
}
public String getLibraryName() {
return libraryName;
}
public void setLibraryName(String libraryName) {
this.libraryName = libraryName;
}
public Collection getBooks() {
return books;
}
public void setBooks(Collection books) {
this.books = books;
}
@Override
public String toString() {
return "[图书馆名=" + libraryName + ", 书名=" + books + "]";
}
public void addBook(Books books) {
this.books.add(books);
}
public void print() {
System.out.println(getLibraryName());
Iterator iterator = this.books.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Books books = (Books) next;
System.out.println(books);
}
}
}
public class Test {
public static void main(String[] args) {
Library library = new Library("图书馆");
library.addBook(new Books("西游记", "吴承恩"));
library.addBook(new Books("水浒传", "施耐庵"));
library.addBook(new Books("红楼梦", "曹雪芹"));
library.addBook(new Books("三国演义", "罗贯中"));
}
}
http://blog.csdn.net/huzongnan/article/list