java类集初学者入门详解

类集概念:

类集:是动态的对象数组,数组是一种,类集可以任意扩充里面的对象内容;
类集必须满足的要求:
1.高性能的框架;
2.框架允许不同类型的类集以相同的方式和高度互操作方式工作;
3.易扩展和修改;
普通的对象数组,受长度的限制;
类集由一系列接口定义其规范,最大的父类接口是.util.Collection;
public interface Collection extends Iterable
开发中很少直接使用该接口,而是使用其字接口实现更细分的规范:
1.List: 成员可以重复 ,
public interface List
extends Collection;// 又称顺序
2.Set:        成员不可重复,判断标准:hashCode、equals方法;
3.Queue:
4.SortedSet:
———.util.List;
1.List: 成员可以重复 ,
public interface List
extends Collection;// 又称顺序
常用子类:
1.public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable;
//public abstract class AbstractCollection extends Object implements Collection
//        public abstract class AbstractList extends AbstractCollection implements List
—覆写方法:
public boolean add(E e) // 追加一个泛型到List;
    Appends the specified element to the end of this list.
public void add(int index,
                E element);        // 在指定位置追加
public boolean addAll(Collection extends E> c);//  追加另一个集合类型
public boolean addAll(int index,
                      Collection extends E> c);//指定位置
public void clear();        // 清空List
public E remove(int index);// 指定位置删除,并返回删除的内容
public boolean remove(Object o);// 按内容匹配删除成功返回true;
public E get(int index);        // 返回指定位置的元素
public E set(int index,
             E element);                // 指定位置设置指定的值
    Replaces the element at the specified position in this list with the specified element.
2.public class Vector
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable;// Vector子类,使用和ArrayList一致;
——–增加元素到List:
package com.mldn;
import .util.List;
import .util.ArrayList;
public class ListDemo
{
        public static void main(String[] args)
        {
                List ls = null;        // 声明List为String型
                ls = new ArrayList();        // 实例化其子类
                ls.add(“Hello, World!”);                // 添加数据 ,在当前指针位置添加一个元素
                ls.add(“We are Chinese!”);                // 添加另一个元素
                ls.add(1, “We say:”);                        // 在指定位置添加一个元素
                List ls1 = new ArrayList();        // 实例化空List
                ls1.add(“by www#tomrrow#com“);
                ls1.add(“致儒先生网”);                                                // 添加内容
                ls.addAll(ls1);                                        // 把ls1List中的所有内容添加到ls中
                ls.addAll(0, ls1);                                // 在List头插入内容;
                System.out.println(ls);        // 调用toString()方法
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.ListDemo
[Hello, World!]
administrator@xu-desktop:~$ java com.mldn.ListDemo
[Hello, World!, We are Chinese!] // toString()方法设计的比较人性化;
administrator@xu-desktop:~$ java com.mldn.ListDemo
[Hello, World!, We say:, We are Chinese!] // 增加到1位置
[Hello, World!, We say:, We are Chinese!, by www#tomrrow#com, 致儒先生网]
administrator@xu-desktop:~$ java com.mldn.ListDemo
[by www#tomrrow#com, 致儒先生网, Hello, World!, We say:, We are Chinese!, by www#tomrrow#com, 致儒先生网]
*/
———-删除List元素:
package com.mldn;
import java.util.List;
import java.util.ArrayList;
public class ListDemo
{
        public static void main(String[] args)
        {
                List ls = null;        // 声明List为String型
                ls = new ArrayList();        // 实例化其子类
                ls.add(“Hello, World!”);                // 添加数据 ,在当前指针位置添加一个元素
                ls.add(“We are Chinese!”);                // 添加另一个元素
                ls.add(1, “We say:”);                        // 在指定位置添加一个元素
                List ls1 = new ArrayList(ls);        // 用List对象实例化List       
                System.out.println(ls);        // 调用toString()方法
                System.out.println(“用List对象实例化的:” + ls1);
                String del = ls1.remove(0);                // 删除List头元素,并返回该元素
                System.out.println(“删除的元素:” + del);
                System.out.println(“删除后List:” + ls1);
                ls.remove(“Hello, World!”);                // 删除匹配内容的元素
                System.out.println(“删除后List:” + ls);
                ls.clear();                // 清空List
                System.out.println(ls);
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.ListDemo
[Hello, World!, We say:, We are Chinese!]
用List对象实例化的:[Hello, World!, We say:, We are Chinese!]
删除的元素:Hello, World!
删除后List:[We say:, We are Chinese!]
[] // 清空后
这两种删除方法都是删除一个元素,按内容删除要求内容和一元素完全匹配!
*/
———输出List:
package com.mldn;
import java.util.List;
import java.util.ArrayList;
public class ListDemo
{
        public static void main(String[] args)
        {
                List ls = null;        // 声明List为String型
                ls = new ArrayList();        // 实例化其子类
                ls.add(“Hello, World!”);                // 添加数据 ,在当前指针位置添加一个元素
                ls.add(“We are Chinese!”);                // 添加另一个元素
                ls.add(1, “We say:”);                        // 在指定位置添加一个元素
                System.out.println(“正序输出:”);
                for (int x = 0; x                 {
                        System.out.print(ls.get(x));        // 获取当前索引的元素
                }
                System.out.println(“\n逆序输出:”);        // 换行
                for (int x = (ls.size() – 1); x >= 0; x–)
                {
                        System.out.print(ls.get(x));        // 获取当前元素
                }               
                System.out.println();        // 换行
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.ListDemo
正序输出:
Hello, World!We say:We are Chinese!
逆序输出:
We are Chinese!We say:Hello, World!
// 根据所引输出元素,是List接口独有的扩展,Collection接口没有,其他非顺序集合也没有;
*/
———将List—》数组并返回:
package com.mldn;
import java.util.List;
import java.util.ArrayList;
public class ListArray
{
        public static void main(String[] args)
        {
                List ls = null;        // 声明List泛型
                ls = new ArrayList();        // 实例化
                ls.add(“Hello,World!”);        // 添加元素
                ls.add(“www#tomrrow#com“);
                System.out.println(“以字符串数组输出:”);
                String[] str = ls.toArray(new String[0]);        // 返回String[]
                for (int x = 0; x                 {
                        System.out.print(str[x]);
                }
                System.out.println(“\n以对象数组输出:”);
                Object[] obj = ls.toArray();        // 返回Object[]
                for (int x = 0; x                 {
                        System.out.print((String)obj[x]);
                }
                System.out.println();        // 换行
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.ListArray
以字符串数组输出:
Hello,World!www#tomrrow#com
以对象数组输出:
Hello,World!www#tomrrow#com
两种方式都可以,后者需要向下转型,可以接收所有类型,前者必须指定正确的类型;
*/
1.Object[] toArray()
    Returns an array containing all of the elements in this list in proper sequence (from first to last element).
2. T[] toArray(T[] a)
    Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
    String[] y = x.toArray(new String[0]);
// 以上两种,是基于数组、基于集合的应用程序之间转换的桥梁;
——–判断List集合是否为空、是否存在与所给内容匹配的元素、返回指定内容匹配的元素的索引:
package com.mldn;
import java.util.List;
import java.util.ArrayList;
public class ListArray
{
        public static void main(String[] args)
        {
                List ls = null;        // 声明清单
                ls = new ArrayList();        // 实例化
                System.out.println(“List集合是否为空:” + (ls.isEmpty() ? “Yes” : “No”));        // 判断是否为空
                ls.add(“Hello”);
                ls.add(“World!”);        // 添加元素
                ls.add(“www.k187.com”);
                System.out.println(“List集合是否为空:” + (ls.isEmpty() ? “Yes” : “No”));        // 判断是否为空
                System.out.println(ls.contains(“Hello”) ? “含有\”Hello\”” : “不含有\”Hello\””);               
                System.out.println((ls.indexOf(“Hello”) != -1) ? “\”Hello\”出现的位置:” + ls.indexOf(“Hello”) : “不存在”);               
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.ListArray
List集合是否为空:Yes
List集合是否为空:No
含有”Hello”                // 该方法要求内容与整个元素匹配,存在返回true!
“Hello”出现的位置:0 // 该方法也要求内容与整个元素匹配,存在返回元素索引;
*/

——Vector与ArrayList一致:
package com.mldn;
import java.util.List;
import java.util.Vector;
public class ListArray
{
        public static void main(String[] args)
        {
                List ls = null;        // 声明List泛型
                ls = new Vector();        // 实例化
                System.out.println(“List集合是否为空:” + (ls.isEmpty() ? “Yes” : “No”));        // 判断是否为空
                ls.add(“Hello”);
                ls.add(“World!”);        // 添加元素
                ls.add(“www.k187.com”);
                System.out.println(“List集合是否为空:” + (ls.isEmpty() ? “Yes” : “No”));        // 判断是否为空
                System.out.println(ls.contains(“Hello”) ? “含有\”Hello\”” : “不含有\”Hello\””);               
                System.out.println((ls.indexOf(“Hello”) != -1) ? “\”Hello\”出现的位置:” + ls.indexOf(“Hello”) : “不存在”);               
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.ListArray
List集合是否为空:Yes
List集合是否为空:No
含有”Hello”                // 该方法要求内容与整个元素匹配,存在返回true!
“Hello”出现的位置:0 // 该方法也要求内容与整个元素匹配,存在返回元素索引;
*/
——–java.util.ArrayList,java.util.Vector区别:
推出 性能 线程安全 输出
1.ArrayList:        JDK1.2以后 异步处理、性能高 非线程安全操作类        Iterator、foreach       
2.Vector:                JDK1.0后 同步处理、性能低 线程安全操作类                Iterator、foreach、Enumeration

———-双链表:java.util.LinkedList;
public class LinkedList
extends AbstractSequentialList
implements List, Deque, Cloneable, Serializable; // 实现了List,Deque等接口;可以理解为双链表;
Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue. // 可以当作栈、队列、双端队列、链表使用!
The class implements the Deque interface, providing first-in-first-out queue operations for add, poll, along with other stack and deque operations.
All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
// 异步处理,当更改其结构(增、删元素)时需要设置同步:
// List list = Collections.synchronizedList(new LinkedList(…)); 该方法比较安全,在实例化时使用;
// 所有实现的接口:
    Serializable, Cloneable, Iterable, Collection, Deque, List, Queue
使用的时候,最好直接以LinkedList形式使用其全部的实现功能;
——-链表元素的添加:支持List接口也有自己的扩展方法;
package com.mldn;
import java.util.LinkedList;
public class LinkedListDemo
{
        public static void main(String[] args)
        {
                LinkedList link = new LinkedList();        // 声明一个空链表,元素是String类型
                // 以追加的形式添加元素
                link.add(“a”);        // 添加元素
                link.add(“b”);
                link.add(“c”);
                System.out.println(“以追加形式添加的链表:” + link);
                // 以插入的形式在指定位置添加元素
                link.addFirst(“head”);        // 在链表头添加
                link.addLast(“end”);        // 在链表尾添加
                link.add(1, “second”);        // 在链表位置2上添加
                System.out.println(“以插入形式添加的链表:” + link);
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.LinkedListDemo
以追加形式添加的链表:[a, b, c]
以插入形式添加的链表:[head, second, a, b, c, end]
链表支持插入,而且可以找到头和尾!
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《java类集初学者入门详解
本文地址:https://www.zhiletu.com/archives-109.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微