Java集合

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java集合相关的知识,希望对你有一定的参考价值。

集合

集合的理解和好处

集合的框架体系

1.集合主要是两组(单列集合,双列集合)

2.Collection 接口有两个重要的子接口 List Set,他们的实现子类都是单列集合

3.Map 接口的实现子类 是双列集合,存放的K-V

Collection接口和常用方法

01:

package HomeWorkD01;

import java.util.ArrayList;
import java.util.List;

public class CollectionDemo {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        List list = new ArrayList();

        list.add("jack");
        list.add(10);
        list.add(true);
        System.out.println(list);

        list.remove(new Integer(10));
        System.out.println(list);

        list.remove("jack");
        System.out.println(list);

        System.out.println(list.contains("jack"));

        System.out.println(list.size());


        System.out.println(list.isEmpty());

        list.clear();
        System.out.println(list);

        List list2 = new ArrayList();
        list2.add("jack");
        list2.add("three countries");

        list.addAll(list2);
        System.out.println(list);

        System.out.println(list.containsAll(list2));

        list.removeAll(list2);
        System.out.println(list);
    }
}

Collection接口遍历元素方式1-iterator(迭代器)

01:

快捷键:itit

all快捷键 ctrl+j

package HomeWorkD01;

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

public class CollectionIterator {
    @SuppressWarnings("all")
    public static void main(String[] args) {

        Collection col = new ArrayList();

        col.add(new Book("three countries","luoguanzhon",10.1));
        col.add(new Book("fly knife of li","old long",5.1));
        col.add(new Book("red city dream","chaoxuex",34.6));

        System.out.println(col);

        Iterator iterator = col.iterator();
        while(iterator.hasNext())
        {
            Object next = iterator.next();
            System.out.println(next);
        }
        
        //itit

//        while (iterator.hasNext()) {
//            Object next =  iterator.next();
//            
//        }
        
        //重置
        iterator = col.iterator();
        
         while(iterator.hasNext())
        {
            Object next = iterator.next();
            System.out.println(next);
        }

    }



}


class Book
{
    private String name;
    private String author;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;




    }


    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\\'' +
                ", author='" + author + '\\'' +
                ", price=" + price +
                '}';
    }
}

Collection接口遍历元素方式2-增强for循环

01:

快捷方式 col.for

package HomeWorkD01;

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

public class CollectionIterator {
    @SuppressWarnings("all")
    public static void main(String[] args) {

        Collection col = new ArrayList();

        col.add(new Book("three countries","luoguanzhon",10.1));
        col.add(new Book("fly knife of li","old long",5.1));
        col.add(new Book("red city dream","chaoxuex",34.6));

        //底层还是迭代器
       for (Object book : col)
       {
           System.out.println(book);
       }

       //也可以在数组使用
        
        int[] nums = {1,2,345,634};
       for (int i:nums)
       {
           System.out.println(i);
       }

       //col.for  快捷方式
//        for (Object o : col) {
//            
//        }
    }



}


class Book
{
    private String name;
    private String author;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;




    }


    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\\'' +
                ", author='" + author + '\\'' +
                ", price=" + price +
                '}';
    }
}

小练习

01:

package HomeWorkD01;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CollectionArrayListHomeWork01
{
    @SuppressWarnings("all")
    public static void main(String[] args) {

        List list = new ArrayList();
        list.add(new Dog("small black",3));
        list.add(new Dog("big yellow",100));
        list.add(new Dog("big strong",8));

        for (Object dog : list) {
            System.out.println(dog);
        }

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object dog =  iterator.next();
            System.out.println(dog);

        }
    }
}

class Dog
{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\\'' +
                ", age=" + age +
                '}';
    }
}

List接口方法和常用方法

01:

package HomeWorkD01;

import java.util.ArrayList;
import java.util.List;

public class ListDemo01 {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("tom");
        list.add("tom");
        list.add("jack");
        list.add("bigstrong");

        //1.list集合类中元素有序
        System.out.println(list);

        //2 and 3.list集合中的每个元素都有其对应的顺序索引,即支持索引
        //索引从0开始
        System.out.println(list.get(3));

        //3.


    }
}

02:

package HomeWorkD01;

import java.util.ArrayList;
import java.util.List;

public class ListMethod {
    @SuppressWarnings("all")
    public static void main(String[] args) {

        List list = new ArrayList();
        list.add("zsf");
        list.add("jby");

        //在index 位置插入ele元素
        //void add(int index,Object ele)

        list.add(1,"hsp");//如果前面没加数字,默认加在最后

        System.out.println(list);

        //boolean addAll(int index,Collection eles)
        //从index位置开始将eles中的所有元素添加进来

        List list2 = new ArrayList();
        list2.add("jack");
        list2.add("tom");
        list.addAll(1,list2);

        //Object get(int index)
        //获取指定index位置的元素

        //int indexOf(Object obj)
        //返回obj在集合中首次出现的位置

        System.out.println(list.indexOf("tom"));

        //int lastIndexOf(Object obj)
        //返回obj在当前集合中末次出现的位置


        System.out.println(list.lastIndexOf("bigstrong"));

        //Object remove(int index)
        //移除指定index位置的元素,并返回此元素

        list.remove(0);
        System.out.println(list);

        //Object set(int index,Object ele)
        //设置指定index位置的元素为ele,相当于是替换

        list.set(1,"mali");

        //List subList(int fromIndex,int toIndex)
        //返回从fromIndex到toIndex位置的子集合  前闭后开
        //fromIndex <= subList < toIndex
        List list1 = list.subList(0,2);

        System.out.println(list1);



    }
}

01:

package HomeWorkD01;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListExercise {
    @SuppressWarnings("all")
    public static void main(String[] args) {

        List list = new ArrayList();
        for (int i= 0;i<12;i++)
        {
            list.add("hello"+i);
        }

        System.out.println(list);

        list.add(1,"hspedu");
        System.out.println(list);

        System.out.println(list.get(4));

        list.remove(5);
        System.out.println(list);

        list.set(6以上是关于Java集合的主要内容,如果未能解决你的问题,请参考以下文章

201621123062《java程序设计》第九周作业总结

Java方法

金蝶handler中 collection 代码片段理解

Alfred常见使用

比较 C# 中的字符串片段并从集合中删除项目

带有红宝石集合/可枚举的酷技巧和富有表现力的片段[关闭]