Learning Java container

table of Contents:

  • ArrayList dynamic array
  • Doubly linked list LinkedLsit
  • Stack Vector, Stack
  • Queue queue, deque
  • 集合TreeSet, HashSet, LinkedHashSet
  • 哈希表HashMap, HashTable, TreeMap, WeakHashMap

A dynamic array

First know the difference between the type of the array of arrays of primitive types and reference:
  1. java the basic data types, including numeric, character, and boolean.
    Numeric: byte; short; int; long .
  2. Type variable reference data comprising: classes, interfaces and array variables
  3. The main difference between the two on storage:
    basic data types when created on the stack to be divided in a memory, the value stored directly on the stack;
    reference data types when the bed frame, to the first on the stack a memory which assigns a reference, and specific information of an object stored on the heap, then the reference point above the stack address objects in a heap.
You know why the array is static?
Java dynamic array of what is meant by:

Generally, the array can not be changed after a defined size. But you need a lot of scenes can change the size of the array. ArrayList is dynamic array legendary, is complicated version of Array, which provides the following several advantages:
1, dynamically increase and decrease the elements
2, to achieve the ICollection and List interfaces
3, the size of the flexible setup arrays
4, ArrayList object is both characteristic of the array, there are features list. You can always add or remove an element from the list.
Such as: ArrayList <String> list = new ArrayList <String> (); then this list can only store String, ArrayList <E>, where <E> is a pan-shaped, i.e., the list can be stored <E> instance.

When to use Array (array), when to use ArrayList?

When we do not know in the end how many data elements, when you can use ArrayList; if you know how many sets of data elements, use an array.

ArrayList common methods:

The following is a summary of some of the more common ArrayList class member method:
Adding elements to the list
boolean add (Element e)
increases the specified element to the tail of the list.
void the Add (int index, the Element E)
increases the specified position specified element to the list.
Remove elements from the list
void clear ()
Removes all elements from the list.
E the Remove (int index)
element position to delete the list specified.
protected void removeRange (int Start, int End)
to delete elements of the list starts from one position to the end of a certain position .
Gets the list of elements
E get (int index)
acquired element list at the specified location.
Object [] toArray ()
Gets an array, the array elements are all elements in the list. (Ie, converted into an array list)
Modify an element
E set (int index, E element )
The element specified position on the list replaced with a new element.
Search element
boolean contains (Object o)
If the list contains the specified elements, returns to true.
int the indexOf (Object O)
returns the position of the element in the first occurrence of a linked list, returns -1 if, the list does not represent this element.
int lastIndexOf (Object o)
returns the position of the element in the list of last occurrence, if -1 indicates that no list elements.
Check if the list is empty
boolean isEmpty ()
Returns true if the list does not have any element.
Gets the list size
int size ()
returns the length of the list (the list contains the number of elements)

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));
    }
}

Second, the list

What is the list?

Published 20 original articles · won praise 1 · views 419

Guess you like

Origin blog.csdn.net/XMY_UPUPUP/article/details/103934269