Java Getting Started---data structure vectors (vector) & Stack (stack) __ Data structures

Source: Internet
Author: User
Tags addall

this time we go on to look at the Java data structure vectors (vector) & Stack (stack). The first is the vector, vectors (vector) classes are very similar to the traditional array, but the size of the vector can be changed dynamically according to the need. As arrays, the elements of a vector object can also be accessed through an index. The main advantage of using the vector class is that when you create an object, you do not have to specify the size of the object, and its size changes dynamically as needed. The vector class implements a dynamic array. and ArrayList and similar, but the two are different:

Vectors are accessed synchronously. Vector contains a number of traditional methods, which do not belong to the collection framework.

Vectors are mainly used in cases where the size of the array is not known beforehand, or only if an array can be changed. The vector class supports 4 construction methods, the first of which creates a default vector with a default size of 10:

Vector ()

the second construction method creates a vector of the specified size:

Vector (int size)

the third construction method creates a vector of the specified size, and the increment is specified with incr. The number of elements added each time by an increment of the vector:

Vector (int size,int incr)

the fourth construct method creates a vector that contains the C element of the collection:

Vector (Collection c)

In addition to the method that inherits from the parent class The vector also defines the following methods:

Serial Number Method Description
1 void Add (int index, Object Element)
Inserts the specified element at the specified position in this vector.
2 Boolean Add (Object o)
Adds the specified element to the end of this vector.
3 Boolean AddAll (Collection c)
Adds all the elements in the specified Collection to the end of this vector, adding the elements in the order returned by the iterator of the specified Collection.
4 Boolean addall (int index, Collection c)
Inserts all the elements in the specified Collection into this vector at the specified location.
5 void AddElement (Object obj)
Adds the specified component to the end of this vector, increasing its size by 1.
6 int capacity ()
Returns the current capacity of this vector.
7 void Clear ()
Removes all elements from this vector.
8 Object Clone ()
Returns a copy of the vector.
9 Boolean contains (Object elem)
Returns true if the vector contains the specified element.
10 Boolean containsall (Collection c)
Returns true if this vector contains all the elements in the specified Collection.
11 void Copyinto (object[] anarray)
Copies the component of this vector to the specified array.
12 Object elementat (int index)
Returns the component at the specified index.
13 Enumeration elements ()
An enumeration that returns the components of this vector.
14 void ensurecapacity (int mincapacity)
Increase the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity parameter.
15 Boolean equals (Object O)
Compares the equality of the specified object with this vector.
16 Object firstelement ()
Returns the item in the first component of this vector (at index 0).
17 Object get (int index)
Returns the element at the specified position in the vector.
18 int Hashcode ()
Returns the hash code value for this vector.
19 int indexOf (Object elem)
Returns the index of the specified element that appears for the first time in this vector, or 1 if the vector does not contain the element.
20 int indexOf (Object elem, int index)
Returns the index of the specified element that appears for the first time in this vector, forward search from index, or 1 if the element is not found.
21st void Insertelementat (Object obj, int index)
Inserts the specified object as a component in this vector at the specified index.
22 Boolean IsEmpty ()
Tests whether this vector does not contain components.
23 Object lastelement ()
Returns the last component of this vector.
24 int LastIndexOf (Object elem)
Returns the index of the last occurrence of the specified element in this vector, or 1 if the vector does not contain the element.
25 int LastIndexOf (Object elem, int index)
Returns the index of the last occurrence of the specified element in this vector, the reverse search from index, or 1 if the element is not found.
26 Object Remove (int index)
Removes the element at the specified position in this vector.
27 Boolean remove (Object o)
Removes the first occurrence of the specified element in this vector, and if the vector does not contain the element, the element remains unchanged.
28 Boolean RemoveAll (Collection c)
Removes all elements contained in the specified Collection from this vector.
29 void Removeallelements ()
Remove all components from this vector and set their size to zero.
30 Boolean removeelement (Object obj)
Removes the first (index smallest) occurrence of a variable from this vector.
31 void Removeelementat (int index)
Deletes the component at the specified index.
32 protected void RemoveRange (int fromindex, int toindex)
Removes all elements from this List that are located between Fromindex (including) and Toindex (not included).
33 Boolean retainall (Collection c)
Only the elements contained in the specified Collection are retained in this vector.
34 Object Set (int index, Object Element)
Replaces the element at the specified position in this vector with the specified element.
35 void Setelementat (Object obj, int index)
Sets the component at index at this vector to the specified object.
36 void setSize (int newsize)
Sets the size of this vector.
37 int size ()
Returns the number of components in this vector.
38 List sublist (int fromindex, int toindex)
Returns a partial view of this List with elements ranging from fromindex (including) to Toindex (not included).
39 Object[] ToArray ()
Returns an array containing all the elements in the vector that are stored in the appropriate order.
40 Object[] ToArray (object[] a)
Returns an array containing all the elements in the vector in the proper order; the Run-time type of the array returns the type of the specified array.
41 String toString ()
Returns a string representation of this vector that contains a string representation of each element.
42 void TrimToSize ()
The capacity of this vector is fine-tuned so that it is equal to the current size of the vector.

The following procedure illustrates several of the methods supported by this collection:

Import java.util.*; public class Vectordemo {public static void main (String args[]) {//initial the size is 3, increment is 2 Ve
      ctor v = new Vector (3, 2);
      System.out.println ("Initial size:" + v.size ());
      System.out.println ("Initial Capacity:" + v.capacity ());
      V.addelement (New Integer (1));
      V.addelement (New Integer (2));
      V.addelement (New Integer (3));
      V.addelement (New Integer (4));

      System.out.println ("Capacity after four additions:" + v.capacity ());
      V.addelement (New Double (5.45));
      System.out.println ("Current Capacity:" + v.capacity ());
      V.addelement (New Double (6.08));
      V.addelement (New Integer (7));
      System.out.println ("Current Capacity:" + v.capacity ());
      V.addelement (New Float (9.4));
      V.addelement (New Integer (10));
      System.out.println ("Current Capacity:" + v.capacity ());
      V.addelement (New Integer (11));
      V.addelement (New Integer (12)); SysTem.out.println ("A:" + (Integer) v.firstelement ());
      System.out.println ("last element:" + (Integer) v.lastelement ());
      if (V.contains (New Integer (3)) System.out.println ("Vector contains 3.");
      Enumerate the elements in the vector.
      Enumeration venum = V.elements ();
      System.out.println ("\nelements in Vector:");
      while (Venum.hasmoreelements ()) System.out.print (venum.nextelement () + "");
   System.out.println (); }
}

the results of the operation are:

Initial size:0
Initial capacity:3
capacity after four additions:5 current
capacity:5 current
capacity : 7 Current
capacity:9-
element:1 last
element:12
Vector contains 3.

Elements in Vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

and then we'll look at the stacks (stack). Stacks (stack) implements a LIFO (LIFO) data structure. You can interpret the stack as a vertical stack of objects, and when you add a new element, place the new element at the top of the other elements. When you take an element from the stack, you take an element from the stack. In other words, the last element in the stack is first taken out. A stack is a subclass of vectors that implements a standard LIFO stack. The stack defines only the default constructor, which is used to create an empty stack. In addition to including all the methods defined by the vector, the stack also defines some of its own methods:

Stack ()

In addition to all the methods defined by the vector, some methods have been defined themselves:

Serial Number Method Description
1 Boolean empty ()
Whether the test stack is empty.
2 Object Peek ()
View the object at the top of the stack, but not remove it from the stack.
3 Object pop ()
Removes the object at the top of the stack and returns the object as the value of this function.
4 Object push (object element)
Press the item onto the top of the stack.
5 int search (Object Element)
Returns the position of an object on the stack, in 1 base.

The following procedure illustrates several of the methods supported by this collection:

Import java.util.*;

public class Stackdemo {

static void Showpush (stack<integer> st, int a) {
St.push (New Integer (a));
System.out.println ("Push (" + A + ")");
System.out.println ("stack:" + st);
}

static void Showpop (Stack<integer> st) {
System.out.print ("Pop->");
Integer a = (integer) st.pop ();
System.out.println (a);
System.out.println ("stack:" + st);
}

public static void Main (String args[]) {
Stack<integer> st = new Stack<integer> ();
System.out.println ("stack:" + st);
Showpush (St, 42);
Showpush (St, 66);
Showpush (ST, 99);
Showpop (ST);
Showpop (ST);
Showpop (ST);
try {
Showpop (ST);
catch (Emptystackexception e) {
System.out.println ("Empty stack");
}
}
}

the results of the operation are:

Stack: [] push stack: [[] Push ($
) stack
: [$]
push stack: [All, $
]
pop- >
stack: [->] pop stack: [the] pop
->
-Stack: []
pop-> empty Stac K
Well, that's it for the time being. If the feeling is good, please give a lot of praise support oh ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.