当前位置: 移动技术网 > IT编程>开发语言>Java > Java集合及LIst接口

Java集合及LIst接口

2019年02月12日  | 移动技术网IT编程  | 我要评论
一、集合的概念 1.概述: 在学习集合前,先回忆一下数组的一个特征 数组有固定的长度,定义一个数组: int[] array = new int[]; 而针对数据长度可变的情况,产生了集合, java集合就是为了应对动态增长数据,在编译时无法知道具体的数据量而产生的。 集合类又叫容器类。 2.集合和 ...

一、集合的概念

 1.概述:

  在学习集合前,先回忆一下数组的一个特征---数组有固定的长度,定义一个数组:

  int[] array = new int[];

  而针对数据长度可变的情况,产生了集合,

  java集合就是为了应对动态增长数据,在编译时无法知道具体的数据量而产生的。

  集合类又叫容器类。

 2.集合和数组的区别

  1>都是容器,数组时固定的长度,集合时可变的;

  2>数组存放的数据都是基本数据类型(四类八种)

   集合存放的数据都是引用数据类型(string、integer、自定义数据类型)

  3>集合中对于基本数据类型会转换位引用数据类型再存储。

 3.集合包含内容、集合的框架

  1>接口:collection,map,set,list等(其中set和list继承了collection)

  2>抽象类:abstractcollection,abstractlist等(实现了部分方法)

  3>实现类:arraylist,linkedlist,hashmap等

  4>迭代器:iterator(集合的访问迭代,返回集合中的元素的迭代器)

二、list集合

 1.概述

  list集合是一个有序的、可重复的集合,集合中每一个元素都有对应的顺序索引。

  list允许加入重复元素是应为可以通过索引来访问指定位置的元素。

  list集合默认按照元素的添加顺序增加元素的索引。

 2.arraylist

  1>概述

   arraylist是基于数组实现的list类,实现所有可选列表操作,允许所有元素包括null

  2>初始化

   arraylist arraylist = new arraylist();  =>初始容量为10的列表集合

   arraylist<e> arraylist = new arraylist<e>(); =>数据类型为e初始容量为10

  3>主要方法

   boolean add(e e) -->将指定的元素追加到此列表的末尾。 
     void add(int index, e element) -->在此列表中的指定位置插入指定的元素。 
     boolean addall(collection<? extends e> c) -->按指定集合的iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾。 
     boolean addall(int index, collection<? extends e> c) -->将指定集合中的所有元素插入到此列表中,从指定的位置开始。
     boolean contains(object o) -->如果此列表包含指定的元素,则返回 true 。
     e get(int index) -->返回此列表中指定位置的元素。 
     e remove(int index) -->删除该列表中指定位置的元素。 
     e set(int index, e element) -->用指定的元素替换此列表中指定位置的元素。
     object[] toarray() -->以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。
 

/**
 * @ author: princesshug
 * @ date: 2019/2/10, 0:18
 * @ blog: https://www.cnblogs.com/hellobigtable/
 */
public class arraylistdemo01 {
    public static void main(string[] args) {
        arraylist<string> arr = new arraylist<string>();
        arr.add("123");
        system.out.println(arr);
        arraylist<person> person = new arraylist<person>();
        person p1 = new person("wyh",18);
        person p2 = new person("hunter", 40);
        person.add(p1);
        person.add(p2);
        for (int i=0;i<person.size();i++) {
            system.out.println(person.get(i));
        }
        system.out.println(person.contains(p2));
        person.remove(1);
        person.set(0,p2);
        person[] persons = new person[2];
        person.toarray(persons);
        system.out.println(persons[0]);
        system.out.println(persons[1]);
    }
}

public class person  {
    private string name;
    private int age;

    public person(){}

    public person(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 "["+this.name+","+this.age+"]";
    }
}

 

 3.list集合遍历的4种方法

  1>通过list.size()方法作为for循环的条件,同数组遍历

  2>通过迭代器iterator

   iterator<integer> it = arraylist.iterator();

   while循环,hasnext作为判断条件,next()获取集合元素再输出。

  3>增强for循环 

  4>jdk1.8新特性foreach

/**
 * @ author: princesshug
 * @ date: 2019/2/12, 2:43
 * @ blog: https://www.cnblogs.com/hellobigtable/
 */
public class printarraylist {
    public static void main(string[] args) {
        arraylist<student> students = new arraylist<>();
        student s1 = new student("001", "wyh", '男');
        student s2 = new student("002", "fyh", '男');
        student s3 = new student("003", "zxy", '男');
        students.add(s1);
        students.add(s2);
        students.add(s3);

        system.out.println("通过size()方法作为for循环条件遍历:");
        for (int i=0;i<students.size();i++){
            system.out.println(students.get(i));
        }

        system.out.println("通过迭代器遍历集合:");
        iterator<student> iterator = students.iterator();
        while (iterator.hasnext()){
            system.out.print(iterator.next() + "\t");
        }

        system.out.println("通过增强for循环遍历集合:");
        for (student stu:students){
            system.out.println(stu);
        }
        system.out.println("通过jdk1.8新特性foreach遍历集合:");
        students.foreach(student -> system.out.println(student));
    }
}

 4.linkedlist

  1>概述:linkedlist指的是链表类的数据结构

    linkedlist与arraylist的区别:

    a)链表中的元素可以任意的增加和删除,但是查询效率不如列表

    b)链表将对象存放在独立的空间中,而且每个空姐保存了下一个链接的索引

  2>构造方法

    linkedlist<e> linkedlist = new linkedlist<e>();

  3>主要方法

void addfirst(e e) -->在该列表开头插入指定的元素。

void addlast(e e) -->将指定的元素追加到此列表的末尾。
e peekfirst() -->检索但不删除此列表的第一个元素,如果此列表为空,则返回 null 。
e peeklast() -->检索但不删除此列表的最后一个元素,如果此列表为空,则返回 null 。
e pollfirst() -->检索并删除此列表的第一个元素,如果此列表为空,则返回 null 。
e pop() -->从此列表表示的堆栈中弹出第一个元素。相似于removefirst() 
void push(e e) -->将元素推送到由此列表表示的堆栈上。相似于addfirst()

/**
 * @ author: princesshug
 * @ date: 2019/2/10, 2:12
 * @ blog: https://www.cnblogs.com/hellobigtable/
 */
public class linkedlistdemo {
    public static void main(string[] args) {
        linkedlist<string> linkedlist = new linkedlist<>();
        linkedlist.addfirst("is");
        linkedlist.addfirst("wyh");
        linkedlist.addlast("cool");
        system.out.println(linkedlist);
        system.out.println(linkedlist.peekfirst());
        system.out.println(linkedlist.pollfirst());
        system.out.println(linkedlist);
        system.out.println(linkedlist.pop());
        system.out.println(linkedlist);
        linkedlist.push("wyh is");
        system.out.println(linkedlist);
    }
}

 

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

相关文章:

验证码:
移动技术网