ArrayList and Vector classes:

Now that we’ve had a rough idea of what sets are, we’ll move on to the inside, starting with ArrayList and Vector.

ArrayList and Vector are array-based List classes, so ArrayList and Vector encapsulate a dynamic, reallocatable Object[] array. An ArrayList or Vector uses the initialCapacity parameter to set the length of the array. When adding elements to an ArrayList or Vector exceeds the length of the array, their initialCapacity is automatically increased.

For normal programming scenarios, the programmer need not care about the initialCapacity of ArrayList or Vector. However, if you are adding a large number of elements to an ArrayList or Vector collection, you can use the ensureCapacity(int minCapacity) method to increase initialCapacity once. This improves performance by reducing the number of reassignments.

If you know from the beginning how many elements the ArrayList or Vector collection needs to hold, you can specify the initialCapacity size when you create them. If initialCapacity is not specified when an empty ArrayList or Vector collection is created, the length of the Object[] array defaults to 10.

In addition, ArrayList and Vector provide two methods for redistributing Object[] arrays.

  • Void ensureCapacity(int minCapacity) : Increments the size of the Object[] array of ArrayList or Vector by minCapacity.
  • Void trimToSize() : Adjusts the size of the Object[] array of ArrayList or Vector to the number of current elements. Programs can call this method to reduce the storage space taken up by ArrayList or Vector collection objects.

ArrayList and Vector are almost identical in usage, but since Vector is an ancient collection (dating back to JDK 1.0), before Java provided a systematic collection framework, Vector provided some methods with very long method names. For example, addElement(Object obj) is no different from add (Object obj). Since JDK 1.2, when Java provided a collection framework for the system, Vector has been changed to implement the List interface as one of the List implementations, resulting in some duplicate methods in Vector.

Tip:

Vector has several methods that duplicate functions. Methods with shorter names were added later, and methods with longer names were the original methods in Vector. Java has rewritten Vector’s original methods, shortening their names to simplify programming. ArrayList started out as the main implementation class for Lists, so there are no methods with long method names. In fact, vectors have many disadvantages, and it is common to implement classes in vectors as little as possible.

The important difference between arrayLists and vectors is that arrayLists are not thread-safe. When multiple threads access the same ArrayList and more than one thread modiates the ArrayList, the program must manually synchronize the ArrayList. Vector collections, on the other hand, are thread-safe and no program is required to ensure that the collection is synchronized. Because Vector is thread-safe, its performance is lower than that of ArrayList. In fact, even if the List collection needs to be thread-safe, it is also not recommended to implement a Vector class. I’ll cover a Collections utility class that can make an ArrayList thread-safe.

Vector also provides a Stack subclass that simulates a data structure called a “Stack,” which is typically a “last in, first out” (LIFO) container. The last element to be “pushed” on the stack will be the first to be “popped” off. As with all collections in Java, objects are pushed in and out of the stack, so fetching elements from the stack must be cast unless you only use operations that Object has. So the Stack class provides the following methods.

  • Object peek() : Returns the first element of the stack, but does not pop it off the stack.

  • Object POP () : Returns the first element on the stack and pops it out

Here’s one arrays.aslist (); ArrayList is a set of fixed length lists.ArrayList is a set of fixed length lists, and the program can only iterate over the elements in the set, but cannot add or delete the elements in the set

ArrayList, and LinkedList

Arraylists are good at random access, but slow at inserting and removing elements in the middle of a List.

LinkedList inserts and deletes are cheap and random access is slow. LinkedList also adds methods that can use stacks, queues, or double-ended queues.

LinkedList also adds methods that allow it to be used as a stack, queue, or double-ended queue (deque). Some of these methods may differ in name only, or only slightly, to make the names more appropriate in the context of a particular usage (especially in Queue). Such as:

  • GetFirst () and Element () are the same in that they both return the head of the List (the first element) without removing it, and throw a NoSuchElementException if the List is empty. The peek() method differs only slightly from these methods in that it returns NULL if the list is empty.
  • RemoveFirst () and remove() are the same, removing and returning the head element of the list and throwing NoSuchElementException if the list is empty. Poll () is slightly different in that it returns NULL if the list is empty.
  • AddFirst () inserts an element at the beginning of the list.
  • Offer () is the same as Add () and addLast(). They all add an element to the end of the list.
  • RemoveLast () removes and returns the last element of the list.

Use LinkedList to implement a stack function:

public class Stack<T> {
	private LinkedList<T> storage = new LinkedList<T>();
	public void push(T v) { storage.addFirst(v); }public T peek(a){ returnstorage.getFirst(); }public T pop(a){ returnstorage.removeFirst(); }public boolean empty(a) { returnstorage.isEmpty(); }public String toString(a) {return storage.toSting();}
}
Copy the code
Construct a stack using LinkedList:
public class Stack<T> {
  private LinkedList<T> storage = new LinkedList<T>();
  public void push(T v) { storage.addFirst(v); }
  public T peek(a) { return storage.getFirst(); }
  public T pop(a) { return storage.removeFirst(); }
  public boolean empty(a) { return storage.isEmpty(); }
  public String toString(a) { returnstorage.toString(); }}/ / / : ~


public class StackTest {
  public static void main(String[] args) {
    Stack<String> stack = new Stack<String>();
    for(String s : "My dog has fleas".split(""))
      stack.push(s);
    while(! stack.empty()) System.out.print(stack.pop() +""); }}/* Output:
fleas has dog My
*// / : ~
Copy the code

Okay, so that’s it for today and there’s a list that you can look at: ArrayList and LinkedList