首页 / JAVA / 学习java第18天
学习java第18天
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了学习java第18天,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4486字,纯文字阅读大概需要7分钟。
内容图文
![学习java第18天](/upload/InfoBanner/zyjiaocheng/628/524a59e138954872ac117ca160e7217d.jpg)
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<>(); ??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")); ??Iterator<Photo> iterator = album.iterator();
??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;
?}
} 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 Queue extends LinkedList
{
?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)); ??h.add("2nd");????????? // 重复元素, 未被加入
??h.add(new Integer(6)); // 重复元素, 未被加入 ??m1(h);
?}
?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", "四"); ??System.out.println( map.get("three") ); ??for( String key : map.keySet() )
???System.out.println( key +":" + map.get(key) ); ??for( String value? : map.values() )
???System.out.println( value ); ??for( Map.Entry<String,String> entry : map.entrySet() )
???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() );
??} ?}
} 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 ); ??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!" );
?}
} class Person
{
?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 );
?}
} 明天学习内容:泛型,常用算法 ?
内容总结
以上是互联网集市为您收集整理的学习java第18天全部内容,希望文章能够帮你解决学习java第18天所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。