java的list详解

本文详细探讨了Java中的List接口,包括其常用方法,并通过实例讲解如何模拟实现ArrayList和LinkedList,帮助读者深化对List的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 List 的常用方法:

public class TestList {
    /**
     * 测试add/remove/size/isEmpty/contains/clear/toArrays等方法
     */
    public static void test01() {
        List<String> list = new ArrayList<String>();
        System.out.println(list.isEmpty()); // true,容器里面没有元素
        list.add("高淇");
        System.out.println(list.isEmpty()); // false,容器里面有元素
        list.add("小七");
        list.add("小八");
        System.out.println(list);
        System.out.println("list的大小:" + list.size());
        System.out.println("是否包含指定元素:" + list.contains("小七"));
        list.remove("小九");
        System.out.println(list); 
        Object[] objs = list.toArray();
        System.out.println("转化成Object数组:" + Arrays.toString(objs));
        list.clear();
        System.out.println("清空所有元素:" + list);
    }
    public static void main(String[] args) {
        test01();
    }
}

2 自己模拟ArrayList

package com.bjsxt.list;
  
public class SxtArrayList {
    private Object[] elementData;//核心数组
    private int size;
 
    public int size() {
    return size;
    }
 
    public boolean isEmpty() {
    return size == 0;
    }
 
    public SxtArrayList() {
    this(10);//调用本类的构造器传入10
    }
 
    public SxtArrayList(int initialCapacity) {
    if (initialCapacity < 0) {
     try {
            throw new Exception();
     } catch (Exception e) {
            e.printStackTrace();
     }
    }
    elementData = new Object[initialCapacity];
    }
 
    public void add(Object obj) {
    // 数组扩容
    if (size == elementData.length) {
     Object[] newArray = new Object[size * 2 + 1];
     System.arraycopy(elementData, 0, newArray, 0, elementData.length);
//实现数组之间的扩容  参数的含义分别是原数组 原数组复制的起始位置,目的数组,目的数组要放置的起始位置,要替换的原数组的长度。
     elementData = newArray;
    }
 
    }
 
    public Object get(int index) {
    rangeCheck(index);
    return elementData[index];
    }
 
    public void remove(int index) {
        rangeCheck(index);
        // 删除指定位置的对象
        // a b c d e
        if (index < 0 || index >= size) {
         try {
            throw new Exception();
         } catch (Exception e) {
            e.printStackTrace();
         }
         int numMoved = size - index - 1;
         if (numMoved > 0) {
            System.arraycopy(elementData,
         index + 1, elementData, index, numMoved);
         }
         elementData[--size] = null;
        }
    }
 
    public void remove(Object obj) {
    for (int i = 0; i < size; i++) {
     if (get(i).equals(obj)) {// 注意:底层调用的equals方法,不是==;
        remove(i);
     }
    }
    }
 
    public Object set(int index, Object obj) {
        rangeCheck(index);
        Object oldValue = elementData[index];
        elementData[index] = obj;
        return oldValue;
    }
 
    private void rangeCheck(int index) {
    if (index < 0 || index >= size) {
     try {
        throw new Exception();
     } catch (Exception e) {
        e.printStackTrace();
     }
    }
    }
 
    public static void main(String[] args) {
        SxtArrayList slist = new SxtArrayList(3);
        slist.add(132);
        slist.add("444");
        slist.add(5);
        slist.add("333");
        slist.add("333");
        slist.add("abc");
        slist.add("ccc");
        System.out.println(slist.size);
        System.out.println(slist.get(6));
    }
}

3 自己模拟LinkList

这块儿写表示节点 构造器


public class SxtLinkedList /*implements List*/ {
       private Node first;
       private Node last;
       
       private int size;
       
       public void add(Object obj){
              Node n = new Node();
       
              if(first==null){
                     n.setPrevious(null);
                     n.setObj(obj);
                     n.setNext(null);
                     
                     first = n;
                     last = n;
              }else{
                     //直接往last节点后增加新的节点
                     n.setPrevious(last);
                     n.setObj(obj);
                     n.setNext(null);
                     
                     last.setNext(n);
                     
                     last = n;
              }
              size++;
       }
       
       public int size(){
              return size;
       }
       
       private void rangeCheck(int index){
              if(index<0||index>=size){
                     try {
                            throw new Exception();
                     } catch (Exception e) {
                            e.printStackTrace();
                     }
              }
       }
       
       public Object get(int index){   //2
              rangeCheck(index);
              
              // 0 1 2 3 4
              Node temp = node(index);
              if(temp!=null){
                     return temp.obj;
              }
              return null;
       }
       
       public Node node(int index){
              Node temp = null;
              if(first!=null){
                     if (index < (size >> 1)) {
                            temp = first;
                            for(int i=0;i<index;i++){
                                   temp = temp.next;
                            }
                     }else{
                            temp = last;
                   for (int i = size - 1; i > index; i--){
                     temp = temp.previous;
                   }
                     }
                     
              }
//            LinkedList l;
              return temp;
       }
       
       
       public void remove(int index){
              Node temp = node(index);
              
              if(temp!=null){
                     Node up = temp.previous;
                     Node down = temp.next;
                     up.next = down;
                     down.previous = up;
                     size--;
              }
              
       }
       
       public void add(int index,Object obj){
              Node temp = node(index);
              
              Node newNode = new Node();
              newNode.obj = obj;
              
              if(temp!=null){
                     Node up = temp.previous;
                     up.next = newNode;
                     newNode.previous = up;
                     
                     newNode.next = temp;
                     temp.previous = newNode;
                     
                     size++;
              }
       }
       
       
       
       
       
       public static void main(String[] args) {
              SxtLinkedList list = new SxtLinkedList();
              list.add("aaa");
              list.add("bbb");
//            list.add(1,"BBBB");
              list.add("ccc");
              list.add("ddd");
              list.add("eee");
//            list.remove(1);
              System.out.println(list.get(3)); 
       }
       
  
}

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值