当前位置: 移动技术网 > IT编程>开发语言>Java > java基础语法集合框架与泛型(List和Set)

java基础语法集合框架与泛型(List和Set)

2020年07月31日  | 移动技术网IT编程  | 我要评论
集合框架与泛型(List和Set)一.集合集合定义二.List接口1.ArrayList2.LinkedList三.Set接口1.HashSet实现类(1)特点(2)基本步骤一.集合集合定义1.集合:解决数组中固定长度不变,无法获取实际存储元素个数,查找效率比较低的问题而引入java集合框架2.Collection接口是最基本的集合接口,可以存储一组不唯一,无序的对象。二.List接口List接口继承Collection接口,是存储可重复的有序的对象1.ArrayList底层是可变数组,遍



在这里插入图片描述


一.集合

集合定义

1.集合:解决数组中固定长度不变,无法获取实际存储元素个数,查找效率比较低的问题而引入java集合框架
2.Collection接口是最基本的集合接口,可以存储一组不唯一,无序的对象。

二.List接口

List接口继承Collection接口,是存储可重复的有序的对象

1.ArrayList

底层是可变数组,遍历元素更快,改变值更快
基本步骤

  • 导入ArrayList类:
    import java.util.ArrayList;
    import java.util.List;
  • 创建ArrayList对象:
    ArrayList list=new ArrayList();
  • 常用方法:
    newsTitleList.add(car);//在集合中增加元素
    newsTitleList.remove(car);//在集合中删除元素
    newsTitleList.get(0);根据下标查找元素
    newsTitleList.set(0,test);//将索引为0的元素替换成test
    newsTitleList.add(3,car);//指定到已插入的位置,则后面的所有元素后移
    newsTitleList.size();元素个数
    list.isEmpty()////判断集合是否为空
    list.clear();//清除list中的数据
    迭代器遍历方法:
    Iterator itr=newsTitleList.iterator();
    while(((Iterator) itr).hasNext()){
    System.out.println(((Iterator) itr).next()+"!!!");
    }
public class New { public static void main(String[] args) { //创建新闻标题对象,NewTitle为新闻标题类 NewTitle car=new NewTitle(1,"汽车","管理员"); NewTitle test=new NewTitle(2,"高考","管理员"); NewTitle person=new NewTitle(3,"人类","哈哈哈"); //创建存储新闻标题的集合对象 ArrayList newsTitleList=new ArrayList(); //按顺序依次添加新闻标题 newsTitleList.add(car);//在集合中增加元素 newsTitleList.add(person); //newsTitleList.remove(car);//在集合中删除元素 // newsTitleList.remove(0); //newsTitleList.add(car); //newsTitleList.size(); //System.out.println(newsTitleList.get(0));//根据下标查找元素 //newsTitleList.set(0,1);//根据下标修改元素 newsTitleList.add(test); //指定到已插入的位置,则后面的所有元素后移 newsTitleList.add(3,car); System.out.println("新闻标题数目为:"+newsTitleList.size()+"条"); System.out.println("新闻标题名称为:"); //1.普通遍历方法 for (int i = 0; i <newsTitleList.size() ; i++) { System.out.println(newsTitleList.get(i)); } //System.out.println(newsTitleList.get(6)); //2.增强for遍历 /*for(Object obj:newsTitleList){
            NewTitle title=(NewTitle)obj;
            System.out.println(title.getID()+": "+title.getName()+" "+title.getTitleName());
        }*/ //3.迭代器遍历 Iterator itr=newsTitleList.iterator(); while(((Iterator) itr).hasNext()){ System.out.println(((Iterator) itr).next()+"!!!"); } /* newsTitleList.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof NewTitle &&o2 instanceof NewTitle){
                    NewTitle n1=(NewTitle) o1;
                    NewTitle n2=(NewTitle) o2;
                    return n1.getTitleID()
                }
                return 0;
            }
        });*/ } } 

2.LinkedList

底层是双向链表,插入,删除更快

  • 导入LinkedList类:import java.util.LinkedList;
  • 创建LinkedList对象:LinkedList list=new LinkedList();
  • 常用方法:list.add(car);
    list.addLast(test);
    list.addFirst(person);
    list.element();//等同于getFirst()
    list.remove();//等同于removeFirst()
    list.offer(“d”);//等同于add(1)
    list.peek();//相当于getFirst(),当前为空就返回空
    list.poll();//弹出首元素,相当于removeFirst()
    list.pop();//等同于removeFirst()
    list.push(“add”);//等同于addFirst()
    遍历:for (int i = 0; i <list.size() ; i++) {
    System.out.println(list.get(i));
    }

三.Set接口

不可重复和无序的对象

1.HashSet实现类

(1)特点

1.集合内的元素是无序的
2.HashSet是线程安全的
3.允许集合元素值为null

(2)基本步骤

  • 导入HashSet类:
    import java.util.Set;
    import java.util.HashSet;
  • 创建HashSet对象:
    Set newTitleList=new HashSet();
  • 常用方法:.
    set.add(1);//增
    set.remove(2)//删
    注:
    不能使用fori形式进行遍历,且没有get()方法
public class TestSet { public static void main(String[] args) { HashSet set=new HashSet(); set.add(1); set.add("abd"); set.add("hello"); set.add("abc"); set.add("abc"); set.add(2); set.iterator(); System.out.println(set.size()); if(set.contains("abc")){ System.out.println(set.remove("abc")); } //1.增强for进行遍历 /* for(Object o:set){
            System.out.println(o);
        }*/ //2.迭代器进行遍历 Iterator itr=set.iterator(); while (itr.hasNext()){ System.out.println(itr.next()); } } } 

本文地址:https://blog.csdn.net/May_J_Oldhu/article/details/107697238

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网