DAY 26

DAY 26,第1张

DAY 26 day26 使用Iterator接口遍历集合元素

遍历元素

package shangGuiGu.day26;


import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class demo01 {
    @Test
            public void test(){
    Collection coll2= new ArrayList();
        coll2.add(123);
        coll2.add(456);
        coll2.add(new String("Tom"));
        coll2.add(false);
        Iterator iterator = coll2.iterator();
        System.out.println(iterator.next());
        for(int i=0;i 
Iterator中的remove 
package shangGuiGu.day26;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class demo02 {
    @Test

    public void test(){
        Collection coll2= new ArrayList();
        coll2.add(123);
        coll2.add(456);
        coll2.add(new String("Tom"));
        coll2.add(false);
        Iterator iterator = coll2.iterator();
        while(iterator.hasNext()){
            Object obj=iterator.next();
            if("Tom".equals(obj)){
                iterator.remove();
            }
        }
        Iterator iterator1= coll2.iterator();
        while(iterator1.hasNext()){
            System.out.println(iterator1.next());
        }
}}
foreach循环

遍历数组、集合

package shangGuiGu.day26;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

public class demo03 {
    @Test
            public void test(){
    Collection coll2= new ArrayList();
        coll2.add(123);
        coll2.add(456);
        coll2.add(new String("Tom"));
        coll2.add(false);
    //for(集合元素的类型  局部变量:集合对象)
        for(Object obj:coll2){
            System.out.println(obj);
        }

    }
}
package shangGuiGu.day26;

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class demo04 {
    @Test
    public void test(){
        ArrayList list = new ArrayList();
        list.add(123);
        list.add(456);
        list.add(new String("Tom"));
        list.add(false);
        list.add(456);
        int index=list.indexOf(456);//返回obj在集合中首次出现的位置,若不存在则返回-1
        System.out.println(index);
        System.out.println(list.lastIndexOf(456));
        System.out.println(list);
        Object remove = list.remove(0);
        System.out.println(remove);
        System.out.println(list);
        list.set(1,"cc");
        System.out.println(list);
        List list2 = list.subList(2, 4);
        System.out.println(list2);
        list.add(1,"BB");
        System.out.println(list);
        List list1 = Arrays.asList(1, 2, 3);
        list.addAll(list1);
        System.out.println(list.size());//8
        System.out.println(list.get(0));
    }}
package shangGuiGu.day26;

import org.junit.Test;
import shangGuiGu.day13.person;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class demo05 {
    @Test
    public void test(){
        Set set=new HashSet();
        set.add(456);
        set.add(123);
        set.add("AA");
        set.add("cc");
        set.add(new person("tom",12));
        set.add(129);
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
package shangGuiGu.day26;

import org.junit.Test;
import shangGuiGu.day13.person;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class demo06 {
    @Test
    public void test(){
        //要求是相同类的对象
        TreeSet set=new TreeSet();
//        错误  set.add(123);
//        set.add(456);
//        set.add("aa");
//        set.add(new person("dd",12));
        set.add(34);
        set.add(-34);
        set.add(24);
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }}
        @Test
                public void test3(){
            Comparator com=new Comparator(){
                @Override
                public int compare(Object o1, Object o2) {
                    return 0;
                }
            };
            TreeSet set=new TreeSet();
            set.add(34);
            set.add(-34);
            set.add(24);
            Iterator iterator=set.iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next());}
        }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5609225.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存