Java vector implementation based on Extensible arrays (algorithm source code)

Source: Internet
Author: User

/* As a master, I would like to share with you as a collector for your reference! */

 

/*
* Vector Interface
*/

Package DSA;

Public interface vector {
// Returns the number of elements in the vector.
Public int getsize ();

// Judge whether the vector is empty
Public Boolean isempty ();

// Elements whose rank is R
Public object getatrank (int r)
Throws exceptionboundaryviolation;

// Replace the element whose rank is R with OBJ
Public object replaceatrank (int r, object OBJ)
Throws exceptionboundaryviolation;

// Insert OBJ as an element whose rank is R. This element is returned.
Public object insertatrank (int r, object OBJ)
Throws exceptionboundaryviolation;

// Delete the element whose rank is R
Public object removeatrank (int r)
Throws exceptionboundaryviolation;
}

 

 

/*
* Array-based vector implementation
*/

Package DSA;

Public class vector_array implements vector {
Private Final int n = 1024; // array capacity
Private int n = 0; // actual scale of the Vector
Private object [] A; // object Array

// Constructor
Public vector_array (){
A = new object [N];
N = 0;
}

// Returns the number of elements in the vector.
Public int getsize () {return N ;}

// Judge whether the vector is empty
Public Boolean isempty () {return (0 = N )? True: false ;}

// Elements whose rank is R
Public object getatrank (int r) // o (1)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Return a [R];
}

// Replace the element whose rank is R with OBJ
Public object replaceatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
A [R] = OBJ;
Return bak;
}

// Insert OBJ as an element whose rank is R. This element is returned.
Public object insertatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
If (n> = N) throw new exceptionboundaryviolation ("unexpected: array overflow ");
For (INT I = N; I> r; I --) A [I] = A [I-1]; // follow-up element shift
A [R] = OBJ; // insert
N ++; // update the current scale
Return OBJ;
}

// Delete the element whose rank is R
Public object removeatrank (int r)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
For (INT I = r; I <n; I ++) A [I] = A [I + 1]; // follow-up element forward sequentially
N --; // update the current scale
Return bak;
}
}

 

/*
* Vector implementation based on Extensible Arrays
*/

Package DSA;

Public class vector_extarray implements vector {
Private int n = 8; // array capacity, which can be continuously increased
Private int N; // the actual scale of the Vector
Private object a []; // object Array

// Constructor
Public vector_extarray () {A = new object [N]; n = 0 ;}

// Returns the number of elements in the vector.
Public int getsize () {return N ;}

// Judge whether the vector is empty
Public Boolean isempty () {return (0 = N )? True: false ;}

// Elements whose rank is R
Public object getatrank (int r)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Return a [R];
}

// Replace the element whose rank is R with OBJ
Public object replaceatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
A [R] = OBJ;
Return bak;
}

// Insert OBJ as an element whose rank is R and return this element.
Public object insertatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
If (n <= N) {// processing of Space Overflow
N * = 2;
Object B [] = new object [N]; // open an array with doubled capacity
For (INT I = 0; I <n; I ++) B [I] = A [I]; // copy the content in a [] to B []
A = B; // replace a with B (the original A [] will be automatically recycled)
}
For (INT I = N; I> r; I --) A [I] = A [I-1]; // follow-up element shift
A [R] = OBJ; // insert
N ++; // update the current scale
Return OBJ;
}

// Delete the element whose rank is R
Public object removeatrank (int r)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
For (INT I = r; I <n-1; I ++) A [I] = A [I + 1]; // follow-up element forward sequentially
N --; // update the current scale
Return bak;
}
}

Related Article

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.