集合

1.

/**
 * 使用Collection接口,增删改查
 * ArrayList内部使用数组实现,变相的实现可变长度的数组,ArrayList能够取代数组。
 * @author Administrator
 *
 */
public class DemoCollection {
    public static void main(String[] args) {
        Collection c  = new ArrayList();
        //1.add方法添加数据
        c.add(1);//存的是Integer对象
        c.add("a");
        c.add(new Person(1,"fyt"));
        
        //2.foreach循环对集合进行遍历
        for (Object object : c) {
            System.out.println(object);
            if(object instanceof Person)
            {
                Person p = (Person)object;
                String name = p.getName();
                System.out.println(name);
            }
        }
        
        //3.size方法获取集合大小。
        int size = c.size();
        
        //3.contains方法比较集合中是否有某个对象
        boolean isContains = c.contains(new Person(1,"fyt"));
        System.out.println(isContains);
        
        //4.clear方法,清除集合中数据
//        c.clear();
        //5.isEmpty方法,判断集合是否为空
        boolean isEmpty = c.isEmpty();
        
        //6.迭代器对象对集合进行遍历
        Iterator it = c.iterator();
        while(it.hasNext())//判断游标右侧是否有数据
        {
            Object obj = it.next();//返回游标右侧,并移动
            if(obj.equals("a"))
                it.remove();
            System.out.println("游标:"+obj);
        }
        
        
        //6.把c2集合中的数据全部添加到c集合中。
        Collection c2 =  new ArrayList();
        c2.add("b");
        c2.add(1);
        
        c.addAll(c2);
        
        System.out.println("c的大小:"+c.size());
        for (Object object : c) {
            System.out.println(object);
        }
        
        //7.把指定的数据从集合中删掉,删除操作量比较大,频繁的移动数据。
        boolean isRemove = c.remove(1);
        System.out.println(" 删除后c的大小:"+c.size());
        
        //8.集合转数组
        Object[] objs = c.toArray();
    }

}
View Code

2.

/**
 * 引入泛型的概念,规范集合中存储的数据类型。
 * @author Administrator
 *
 */
public class DemoCollection2 {
    public static void main(String[] args) {
        //<String> 说明集合中只能存储String字符串
        Collection<String> c = new ArrayList<String>();
        c.add("a");
        c.add("sdb");
        
        for (String str : c) {
            System.out.println(str);
        }
        
        
        Collection<Person> c2 = new ArrayList<Person>();
        c2.add(new Person(1,"aaa"));
        c2.add(new Person(2,"bbbb"));
        
        
        for (Person p : c2) {
            System.out.println(p.getId()+"--"+p.getName());
        }
    }

}
View Code

3.

public class DemoCopy {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("z");
        list.add("a");
        list.add("c");
        list.add("b");
        
        List<String> newList= new ArrayList<String>();
        newList.add("as");
        newList.add("as");
        newList.add("as");
        newList.add("as");
        newList.add("as");
        newList.add("as");
        //要求新集合的长度必须大于等于原集合
        Collections.copy(newList, list);
        
        for (String string : newList) {
            System.out.println(string);
        }
    }

}
View Code

4.

/**
 * Collections.sort(List list)要求list集合中对象时可排序
 * Collections.sort(List list,Comparator comparator)   自定义排序器对象
 * @author Administrator
 *
 */
public class DemoSort {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("z");
        list.add("a");
        list.add("c");
        list.add("b");
        
        System.out.println("-------使用sort方法对List集合进行排序-----");
        Collections.sort(list);
        
        for (String string : list) {
            System.out.println(string);
        }
        
        List<Book> bookList = new ArrayList<Book>();
        bookList.add(new Book(12,"a" ,1));
        bookList.add(new Book(14,"b" ,1));
        bookList.add(new Book(13,"d" ,1));
        bookList.add(new Book(15,"c" ,2));
        bookList.add(new Book(16,"c" ,4));
        
        Collections.sort(bookList, new BookComparator());
        
        for (Book book : bookList) {
            System.out.println(book.getId()+"--"+book.getPrice());
        }
        
        
    }

}
View Code

5.

/**
 * Collection接口中的toArray方法,转数组
 * Arrays工具类中的asList方法,转集合
 * @author Administrator
 *
 */
public class Demo {
    public static void main(String[] args) {
        //集合转数组
        List<String> list=  new ArrayList<String>();
        String[] ss = new String[list.size()];
        list.toArray(ss);
        
        String[] ss2 = list.toArray(new String[list.size()]);
        
        //数组转集合
        List<String> list2 = Arrays.asList(ss);
    }

}
View Code
今天多一点积累,明天少一分烦恼
原文地址:https://www.cnblogs.com/galibujianbusana/p/5712676.html