Java-based array customization for capacity-invariant vector vectors

Source: Internet
Author: User

background: Suppose that the set S consists of n elements, which are stored in a linear order, so that we can directly access the first element, the second element, the third element ... That is, each integer between [0, n-1] can be accessed directly to the unique element e, and this integer is equal to the number of elements in S before e?? Here, we call this element rank (rank). It is not difficult to see that if the rank of element e is R, then as long as the direct precursor of E (or direct successor) exists, its rank is r-1 (or r+1). This definition is consistent with the numbering sequence for array elements in programming languages such as Java and C + +. A sequence that supports direct access to its elements via rank, called a vector or array list. In fact, the intuitive concept of rank is very powerful, and it can directly specify where to insert or delete elements.

the vector ADT methods are defined as follows:

Based on the array, vector ADT can be implemented directly. We borrow an array of array[], where Array[i] holds a reference, pointing to a vector element with rank position. For this reason, array[] 's capacity capacity need to be large enough to set an instance variable size to indicate the actual size of the vector.

Specific code implementation:
Vector Interface:

/** * Vector Interface * / PackageCom.vector_and_array;/** * @author gannyee * */ Public  interface vectorinterface {    //get size of vector     Public int GetSize();//is Empty     Public Boolean IsEmpty();//get element at rank     PublicObjectGetatrank(intPositionthrowsExceptionboundaryexceed;//replace element at rank     Public void Replaceatrank(intPosition,object Element)throwsExceptionboundaryexceed;//insert element at rank     Public void Insertatrank(intPosition,object Element)throwsExceptionboundaryexceed;//remove element at rank     Public void Removeatrank(intPositionthrowsExceptionboundaryexceed;//travel all elements     Public void getallelements();}

Custom Exceptionboundaryexceed class:

package com.vector_and_array;/** * @author gannyee * Exception for boundaryExceed */publicclass ExceptionBoundaryExceed extends Exception{    //Constructor overload    publicExceptionBoundaryExceed(String message){        super(message);    }}

Vector Concrete Implementation
Vectorbasiconarray class:

 PackageCom.vector_and_array;ImportJava.util.Arrays; Public  class vectorbasiconarrays implements vectorinterface {    //define static variable    Private Static Final intCapacity =1024x768;//declared size of vector    Private Static intSize//declared array for vectorObject[] Array;//constructor     Public vectorbasiconarrays() {size =0; Array =NewObject[capacity]; }@Override     Public int GetSize() {returnSize }@Override     Public Boolean IsEmpty() {returnSize = =0; }@Override     PublicObjectGetatrank(intPositionthrowsexceptionboundaryexceed{if(Position <0|| Position > Size)Throw NewExceptionboundaryexceed ("Vector exceed!");returnArray[position]; }@Override     Public void Replaceatrank(intPosition, Object Element)throwsexceptionboundaryexceed{if(Position <0|| Position > Size)Throw NewExceptionboundaryexceed ("Vector exceed!");        Array[position] = element; System.out.println ("Element replaced is:"+ Array[position] +"at position:"+ position); }@Override     Public void Insertatrank(intPosition, Object Element)throwsexceptionboundaryexceed{if(Position <0|| Position > Size)Throw NewExceptionboundaryexceed ("Vector exceed!");if(Size > capacity)Throw NewExceptionboundaryexceed ("Vector overflow!"); for(inti = Size;i >= position;i--) Array[i +1] = Array[i];        Array[position] = element;        Size + +; System.out.println ("Element You insert is:"+ element +"at position:"+ position); }@Override     Public void Removeatrank(intPositionthrowsexceptionboundaryexceed{if(Position <0|| Position > Size)Throw NewExceptionboundaryexceed ("Vector exceed!"); System.out.println ("Element You remove is:"+ array[position]); for(inti = position;i <= size-1; i + +) array[i] = Array[i +1];    Size--; }@Override     Public void getallelements() {object[] Arraytravel =NewObject[size]; for(inti =0; i < Arraytravel.length;i + +) arraytravel[i] = Array[i]; System.out.println ("All elements is:"+ arrays.tostring (arraytravel)); }}

Test code:

Packagecom. Vector_and_array;/** * Test * @author gannyee * */public class Vectortest {public static void main (string[] args) throws Exceptionboundaryexceed {//define Vecto Rbasiconarrat class Vectorbasiconarrays VBA = new Vectorbasiconarrays ();System. out. println("Size:"+ VBA. GetSize());System. out. println("is empty?"+ VBA. IsEmpty());Vba. Insertatrank(0,1);Vba. Insertatrank(1,2);Vba. Insertatrank(2,3);Vba. Insertatrank(3,4);Vba. Insertatrank(4,5);Vba. Insertatrank(5,6);Vba. Insertatrank(5,7);Vba. Insertatrank(6,8);System. out. println("Size:"+ VBA. GetSize());System. out. println("is empty?"+ VBA. IsEmpty());Vba. Getallelements();Vba. Replaceatrank(0, A);Vba. Replaceatrank(1, -);Vba. Replaceatrank(2, -);Vba. Replaceatrank(3, the);System. out. println("Size:"+ VBA. GetSize());System. out. println("is empty?"+ VBA. IsEmpty());Vba. Getallelements();Vba. Removeatrank(7);Vba. Removeatrank(5);Vba. Removeatrank(4);Vba. Removeatrank(1);Vba. Removeatrank(0);System. out. println("Size:"+ VBA. GetSize());System. out. println("is empty?"+ VBA. IsEmpty());Vba. Getallelements();}}

Test results:

Size:0is empty? TrueelementInsert  is:1  atPosition0Element YouInsert  is:2  atPosition1Element YouInsert  is:3  atPosition2Element YouInsert  is:4  atPosition3Element YouInsert  is:5  atPosition4Element YouInsert  is:6  atPosition5Element YouInsert  is:7  atPosition5Element YouInsert  is:8  atPosition6Size:8 isEmpty?false AllElements is: [1,2,3,4,5,7,8,6]element replaced is: A  atPosition0Element replaced is: -  atPosition1Element replaced is: -  atPosition2Element replaced is: the  atPosition3Size:8 isEmpty?false AllElements is: [ A, -, -, the,5,7,8,6]element you remove is:6Element you remove is:7Element you remove is:5Element you remove is: -Element you remove is: ASize:3 isEmpty?false AllElements is: [ -, the,8]

Related text reference data structure and algorithm (Java description) Deng Junhui

not to be continued ....
Vector vector! with variable capacity based on array customization

SOURCE on GitHub: https://github.com/gannyee/JavaDataStruct

Java-based array customization for capacity-invariant vector vectors

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.