学习java第18天
2021-04-10 11:27
标签:extends orm eth ddl library util enqueue room compareto 1.集合 *Collection接口: List : 记录元素保存顺序,且允许有重复元素 Set :不 记录元素保存顺序,且不允许有重复元素 2.List 主要的实现类ArrayList.LinkedList 3.迭代器 Iterator iterator = iterable.inerator(); while(iterator.hasNext()) dosomething(iterator.next()); 4.增强的for语句 学习java第18天 标签:extends orm eth ddl library util enqueue room compareto 原文地址:https://www.cnblogs.com/SirNie/p/13367550.html
class TestList {
public static void main(String[] args){
//List
List
album.add( new Photo("two",new Date(), "library"));
album.add( new Photo("three",new Date(), "gym"));
album.add( new Photo("three",new Date(), "dorm"));
while(iterator.hasNext()){
Photo photo = iterator.next();
System.out.println( photo.toString() );
}
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;
}
}
class TestQueue
{
public static void main(String[] args)
{
Queue q = new Queue();
for( int i=0; i q.enqueue( ""+i );
while( ! q.isEmpty() )
System.out.println( q.dequeue() );
}
}
{
void enqueue( Object obj ){
addLast( obj );
}
Object dequeue(){
return removeFirst();
}
public boolean isEmpty(){
return super.isEmpty();
}
}
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(new Integer(6)); // 重复元素, 未被加入
}
public static void m1(Set s){
System.out.println(s); // 调用了其toString()方法,注意显示时,元素无顺序
}
}
class TestHashMap
{
public static void main( String[] args){
//Map
Map
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( key +":" + map.get(key) );
System.out.println( value );
System.out.println( entry.getKey() +":" + entry.getValue() );
while(it.hasNext()){
Map.Entry
System.out.println( entry.getKey() +":" + entry.getValue() );
}
}
class TestCollectionsSort
{
public static void main(String[] args)
{
List
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 );
school, new Person("Li",23), new PersonComparator() );
if( index >=0 )
System.out.println( "Found:" + school.get( index ));
else
System.out.println( "Not Found!" );
}
}
{
String name;
int age;
public Person( String name, int age){
this.name=name;
this.age=age;
}
public String toString(){
return name+":"+age;
}
}
{
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
}
}