First, you plan the member variables and the methods that are owned by the array class and the type of the array:
1. The type of the array is int (note: will be changed to generics later, support various data types)
2. The member variables of an array include the size of an array of type int and arrays
3. The methods of the array include: the capacity of the constructor method to initialize the array;
Determines whether the array is empty;
Increase:
Insert element at specified position
Insert first position
Insert last Position
By deleting:
Delete location Insert Element
Delete First position
Delete last position
Change:
Modifies the element at the specified position
Check: Find the element at the specified location
The code is as follows:
PackageArray;/*** @Author: Meteor @Date: 2018/7/13 23:41 * Design a normal array (non-generic: int)*/ Public classArray {//Private Properties Private int[] data;//Array Private intSize//the size of the array//constructor: Give the initialization capacity of an array PublicArrayintcapacity) {Data=New int[capacity]; } //encapsulating a parameterized construction method into a non-parametric construction method PublicArray () { This(10); Size= 0; } //determine if the array is empty Public BooleanIsEmpty () {returnSize = = 0; } //the size of the array Public intGetSize () {returnsize; } //add elements to all elements in an array Public voidAddLast (intElement) { //You can reuse the Add (index,element) methodAdd (size,element); /*if (size = = Data.length) {throw new IllegalArgumentException ("AddLast fail, array is full"); } Data[size] = element; Size + +;*/ } //add an element before all the elements Public voidAddFirst (intElement) { //reusing the Add (index,element) methodAdd (0, Element); } //insert element under given index Public voidAddintIndexintElement) { if(Size = =data.length) { Throw NewIllegalArgumentException ("AddLast fail, array is full"); } if(Index < 0){ Throw NewIllegalArgumentException ("AddLast fail, index < 0"); } //copy from back to forward for(inti = size; i > index; i--) {Data[i]= Data[i-1]; } Data[index]=element; //to maintain sizesize++; } //View whether the element is contained in the array: Returns TRUE to return false, not found Public BooleanContainsintElement) { //iterating through an array for(intitem:data) { if(Data[item] = =Element)return true; } return false; } //find the index for the element: Returns the subscript, does not find the return-1 Public intFindintElement) { for(intitem:data) { if(Data[item] = =Element)returnitem; } return-1; } //removes the specified element from the array and returns the deleted element Public intRemoveintindex) { if(Index <0 | | index >=size) { Throw NewIllegalArgumentException ("Get fail, index is illegal"); } intRET =Data[index]; for(inti = index+1;i<size;i++) {Data[i-1] =Data[i]; } size--; returnret; } /*** Removes the first element from the array and returns the deleted element*/ Public intRemovefirst () {returnRemove (0); } /*** Remove the last element from the array and return the deleted element *@return */ Public intRemovelast () {returnRemove (size-1); } /*** Delete an element in an array *@paramelement*/ Public voidRemoveelement (intElement) { //first determine if the element exists intindex =find (Element); if(Index! =-1) {remove (index); } } //gets the element that corresponds to the index intGetintindex) { if(Index <0 | | index >=size) { Throw NewIllegalArgumentException ("Get fail, index is illegal"); } returnData[index]; } //change the value of the corresponding element for a given index voidSetintIndexintElement) { if(Index <0 | | index >=size) { Throw NewIllegalArgumentException ("Set fail, index is illegal"); } Data[index]=element; } //the method of using StringBuilder to splice ToString@Override PublicString toString () {StringBuilder s=NewStringBuilder (); S.append (String.Format ("Array:size=%d,capacity=%d\n", size,data.length)); S.append ("["); for(inti=0;i<size;i++) {s.append (data[i]); if(I! = size-1) S.append (","); } s.append ("]"); returns.tostring (); }}
Data structure and algorithm ——— hands to teach you to implement arrays (Java language)