list = new ArrayList();" />

"景先生毕设|www.jxszl.com

ArrayList方法代码示例

2023-09-12 15:40编辑: www.jxszl.com景先生毕设

ArrayList方法代码示例

ArrayList 概述: ArrayList是List接口的可变数组的实现, 每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小
 
ArrayList类支持3个构造方法:
Arraylist()构造了一个空的链表。
ArrayList(Collection<? extends E> c);构造了一个包含指定元素集合的链表,注意,这里的字符E是一个标记,用来表示集合中元素的类型。
ArrayList(int initialCapacity)
 
1.for循环下标遍历:迭代器(Iterator和ListIterator);
package com.pichen.basis.col;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
 
public class Test {
 
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < 10; i++){
            list.add(i);
        }
        
        //直接打印
        System.out.println(list.toString());
        
        //for循环
        System.out.println("for循环:");
        for(int i = 0; i < list.size(); i++){
            System.out.print(list.get(i) + " ");
        }
        
        //foreach
        System.out.println("\nforeach:");
        for(Integer i : list){
            System.out.print(i + " ");
        }
        
        //iterator
        System.out.println("\niterator:");
        Iterator<Integer> iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        
        //listIterator
        System.out.println("\nlistIterator:");
        ListIterator<Integer> listIterator = list.listIterator();
        while(listIterator.hasNext()){
            System.out.print(listIterator.next() + " ");
        }
        System.out.println();
        while(listIterator.hasPrevious()){
            System.out.print(listIterator.previous() + " ");
        }
    }
}
 
2.添加元素:
 
/*在指定位置添加一个元素**/
public void add(int index, E element) {  
    rangeCheckForAdd(index);  
  
    ensureCapacityInternal(size + 1);  // Increments modCount!!  
    System.arraycopy(elementData, index, elementData, index + 1,  
                     size - index);  
    elementData[index] = element;  
    size++;  
}  
 
 
/*添加一个集合**/
public boolean addAll(Collection<? extends E> c) {
    //把该集合转为对象数组
    Object[] a = c.toArray();
    int numNew = a.length;
    //增加容量
    ensureCapacityInternal(size + numNew);  // Increments modCount
    //挨个向后迁移
    System.arraycopy(a, 0, elementData, size, numNew);
    size += numNew;
    //新数组有元素,就返回 true
    return numNew != 0;
}
 
3.将集合转化为数组:
 
public Object[] toArray() {  
    return Arrays.copyOf(elementData, size);  
}  
 
4.常用方法示例:
import java.util.*;
 
public class ArrayListExamples {
 
    public static void main(String args[]) {
        // 创建一个空的数组链表对象list,list用来存放String类型的数据
        ArrayList<String> list = new ArrayList<String>();
 
        // 增加元素到list对象中
        list.add("Item1");
        list.add("Item2");
        list.add(2, "Item3"); // 此条语句将会把“Item3”字符串增加到list的第3个位置。
        list.add("Item4");
 
        // 显示数组链表中的内容
        System.out.println("The arraylist contains the following elements: "
                + list);
 
        // 检查元素的位置
        int pos = list.indexOf("Item2");
        System.out.println("The index of Item2 is: " + pos);
 
        // 检查数组链表是否为空
        boolean check = list.isEmpty();
        System.out.println("Checking if the arraylist is empty: " + check);
 
        // 获取链表的大小
        int size = list.size();
        System.out.println("The size of the list is: " + size);
 
        // 检查数组链表中是否包含某元素
        boolean element = list.contains("Item5");
        System.out
                .println("Checking if the arraylist contains the object Item5: "
                        + element);
 
        // 获取指定位置上的元素
        String item = list.get(0);
        System.out.println("The item is the index 0 is: " + item);
 
        // 遍历arraylist中的元素
 
        // 第1种方法: 循环使用元素的索引和链表的大小
        System.out
                .println("Retrieving items with loop using index and size list");
        for (int i = 0; i < list.size(); i++) {
            System.out.println("Index: " + i + " - Item: " + list.get(i));
        }
 
        // 第2种方法:使用foreach循环
        System.out.println("Retrieving items using foreach loop");
        for (String str : list) {
            System.out.println("Item is: " + str);
        }
 
        // 第三种方法:使用迭代器
        // hasNext(): 返回true表示链表链表中还有元素
        // next(): 返回下一个元素
        System.out.println("Retrieving items using iterator");
        for (Iterator<String> it = list.iterator(); it.hasNext();) {
            System.out.println("Item is: " + it.next());
        }
 
        // 替换元素
        list.set(1, "NewItem");
        System.out.println("The arraylist after the replacement is: " + list);
 
        // 移除元素
        // 移除第0个位置上的元素
        list.remove(0);
 
        // 移除第一次找到的 "Item3"元素
        list.remove("Item3");
 
        System.out.println("The final contents of the arraylist are: " + list);
 
        // 转换 ArrayList 为 Array
        String[] simpleArray = list.toArray(new String[list.size()]);
        System.out.println("The array created after the conversion of our arraylist is: "
                        + Arrays.toString(simpleArray));
    }
}
JAVA/253.html">http://www.itemperor.com/a/JAVA/253.html
 

原文链接:http://www.jxszl.com/biancheng/JAVA/446605.html