1.集合
*Collection接口:
List : 记录元素保存顺序,且允许有重复元素
Set :不 记录元素保存顺序,且不允许有重复元素
2.List
主要的实现类ArrayList.LinkedList
3.迭代器
Iterator iterator = iterable.inerator();
while(iterator.hasNext()) dosomething(iterator.next());
4.增强的for语句
import java.util.*;
class TestList {
public static void main(String[] args){
//List<Photo> album = new ArrayList<>();
List<Photo> album = new LinkedList<>();
class TestList {
public static void main(String[] args){
//List<Photo> album = new ArrayList<>();
List<Photo> album = new LinkedList<>();
album.add( new Photo("one",new Date(), "classroom"));
album.add( new Photo("two",new Date(), "library"));
album.add( new Photo("three",new Date(), "gym"));
album.add( new Photo("three",new Date(), "dorm"));
album.add( new Photo("two",new Date(), "library"));
album.add( new Photo("three",new Date(), "gym"));
album.add( new Photo("three",new Date(), "dorm"));
Iterator<Photo> iterator = album.iterator();
while(iterator.hasNext()){
Photo photo = iterator.next();
System.out.println( photo.toString() );
}
while(iterator.hasNext()){
Photo photo = iterator.next();
System.out.println( photo.toString() );
}
for( Photo photo : album ){
System.out.println( photo );
}
}
}
class Photo {
String title;
Date date;
String memo;
Photo(String title, Date date, String memo){
this.title = title;
this.date = date;
this.memo = memo;
}
@Override
public String toString(){
return title + "(" + date + ")" + memo;
}
}
System.out.println( photo );
}
}
}
class Photo {
String title;
Date date;
String memo;
Photo(String title, Date date, String memo){
this.title = title;
this.date = date;
this.memo = memo;
}
@Override
public String toString(){
return title + "(" + date + ")" + memo;
}
}
5.Stack
遵循 “后进先出”原则
包含三个方法: #public Object push(Object item) : 将指定对象压入栈中
#Public Object pop() : 将栈最上面的元素从栈中取出,并返回这个对象
#public boolean empty() : 判断栈中有没有对象元素
6.队列
遵循“先进后出”的原则
入队:固定在一端输入数据 出队:在一端输出数据
import java.util.*;
class TestQueue
{
public static void main(String[] args)
{
Queue q = new Queue();
for( int i=0; i<5; i++ )
q.enqueue( ""+i );
while( ! q.isEmpty() )
System.out.println( q.dequeue() );
}
}
class TestQueue
{
public static void main(String[] args)
{
Queue q = new Queue();
for( int i=0; i<5; i++ )
q.enqueue( ""+i );
while( ! q.isEmpty() )
System.out.println( q.dequeue() );
}
}
class Queue extends LinkedList
{
void enqueue( Object obj ){
addLast( obj );
}
Object dequeue(){
return removeFirst();
}
public boolean isEmpty(){
return super.isEmpty();
}
}
{
void enqueue( Object obj ){
addLast( obj );
}
Object dequeue(){
return removeFirst();
}
public boolean isEmpty(){
return super.isEmpty();
}
}
7.Set集
*HashSet
import java.util.*;
public class TestHashSet {
public static void main(String[] args) {
Set h = new HashSet();
h.add("1st");
h.add("2nd");
h.add("3rd");
h.add("4th");
h.add("5th");
h.add(new Integer(6));
h.add(new Double(7.0));
public class TestHashSet {
public static void main(String[] args) {
Set h = new HashSet();
h.add("1st");
h.add("2nd");
h.add("3rd");
h.add("4th");
h.add("5th");
h.add(new Integer(6));
h.add(new Double(7.0));
h.add("2nd"); // 重复元素, 未被加入
h.add(new Integer(6)); // 重复元素, 未被加入
h.add(new Integer(6)); // 重复元素, 未被加入
m1(h);
}
public static void m1(Set s){
System.out.println(s); // 调用了其toString()方法,注意显示时,元素无顺序
}
}
}
public static void m1(Set s){
System.out.println(s); // 调用了其toString()方法,注意显示时,元素无顺序
}
}
8.Map
*HashMap
import java.util.*;
class TestHashMap
{
public static void main( String[] args){
//Map<String, String> map = new HashMap<String, String>();
Map<String, String> map = new TreeMap<String, String>();
map.put("one", "一");
map.put("two", "二");
map.put("three", "三");
map.put("four", "四");
map.put(new String("five"), "五");
map.put(new String("five"), "五二");
map.put("six", "四");
class TestHashMap
{
public static void main( String[] args){
//Map<String, String> map = new HashMap<String, String>();
Map<String, String> map = new TreeMap<String, String>();
map.put("one", "一");
map.put("two", "二");
map.put("three", "三");
map.put("four", "四");
map.put(new String("five"), "五");
map.put(new String("five"), "五二");
map.put("six", "四");
System.out.println( map.get("three") );
for( String key : map.keySet() )
System.out.println( key +":" + map.get(key) );
System.out.println( key +":" + map.get(key) );
for( String value : map.values() )
System.out.println( value );
System.out.println( value );
for( Map.Entry<String,String> entry : map.entrySet() )
System.out.println( entry.getKey() +":" + entry.getValue() );
System.out.println( entry.getKey() +":" + entry.getValue() );
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,String> entry = (Map.Entry<String,String>)it.next();
System.out.println( entry.getKey() +":" + entry.getValue() );
}
while(it.hasNext()){
Map.Entry<String,String> entry = (Map.Entry<String,String>)it.next();
System.out.println( entry.getKey() +":" + entry.getValue() );
}
}
}
}
9.排序和查找
*Arrays类
用于对数组进行排序和搜索的类
Arrays类提供了sort()和binarySearch()
*Collection类
import java.util.*;
class TestCollectionsSort
{
public static void main(String[] args)
{
List<Person> school = new ArrayList<Person>();
school.add( new Person("Li",23));
school.add( new Person("Wang",28));
school.add( new Person("Zhang",21));
school.add( new Person("Tang",19));
school.add( new Person("Chen",22));
school.add( new Person("Zhao",22));
System.out.println( school );
Collections.sort( school, new PersonComparator() );
System.out.println( school );
class TestCollectionsSort
{
public static void main(String[] args)
{
List<Person> school = new ArrayList<Person>();
school.add( new Person("Li",23));
school.add( new Person("Wang",28));
school.add( new Person("Zhang",21));
school.add( new Person("Tang",19));
school.add( new Person("Chen",22));
school.add( new Person("Zhao",22));
System.out.println( school );
Collections.sort( school, new PersonComparator() );
System.out.println( school );
int index = Collections.binarySearch(
school, new Person("Li",23), new PersonComparator() );
if( index >=0 )
System.out.println( "Found:" + school.get( index ));
else
System.out.println( "Not Found!" );
}
}
school, new Person("Li",23), new PersonComparator() );
if( index >=0 )
System.out.println( "Found:" + school.get( index ));
else
System.out.println( "Not Found!" );
}
}
class Person
{
String name;
int age;
public Person( String name, int age){
this.name=name;
this.age=age;
}
public String toString(){
return name+":"+age;
}
}
{
String name;
int age;
public Person( String name, int age){
this.name=name;
this.age=age;
}
public String toString(){
return name+":"+age;
}
}
class PersonComparator implements Comparator
{
public int compare( Object obj1, Object obj2 ){
Person p1 = (Person)obj1;
Person p2 = (Person)obj2;
if( p1.age > p2.age ) return 1;
else if(p1.age<p2.age) return -1;
return p1.name.compareTo( p2.name );
}
}
{
public int compare( Object obj1, Object obj2 ){
Person p1 = (Person)obj1;
Person p2 = (Person)obj2;
if( p1.age > p2.age ) return 1;
else if(p1.age<p2.age) return -1;
return p1.name.compareTo( p2.name );
}
}
明天学习内容:泛型,常用算法