Custom Arrays (object-oriented programming):
Directly on the code:
package com.xingej.algorithm.datastructure.array;/** * Object-Oriented programming * * custom class arrays * * You must understand that this is a package based on the JDK, to learn this idea, other frameworks such as netty are also on the basis of the former package, if there is a * * Part is the encapsulated thread pool * * @author erjun 2017 November 28 PM 9:42:10 */public class MyArray { // defined data structure, type is array // This is the most core component, and the others are private long[] arr; //around this. Indicates the length of valid data, that is, how many arrays are in the array private int elements; Public myarray () { // can store 50 digits by default arr = new long[50]; } public myarray (int maxsize) { arr = new long[maxsize]; } // Inserting Data public void insert (Long value) { arr[elements] = value; // each time the data is inserted, it will be added once elements++; } // find data by index public long get (Int index) { if (index >= elements | | index < 0) { Throw new arrayindexoutofboundsexception (); } return arr[index]; } // display data, that is, the contents of the printed array public void display () { // description, there is no content in the array at this time if (elements <= 0) { return; } system.out.print ("[ "); for (int i = 0; i < elements; i++) { system.out.print (arr[i] + " "); } system.out.println ("]"); } // returns the index value based on the value entered public int Getindexbyvalue (Long value) { int i = 0; for (; i < elements; i++) { if (Arr[i] == value) { // returns the index value immediately if it is found return i; } } return -1; } // Delete the elements in the array according to the index value public void delete (int index) { if (index >= elements | | index < 0) { Throw new arrayindexoutofboundsexcEption (); } // Move the elements behind, moving forward for (int i = index; i < elements; i++) { arr[i] = arr[i + 1]; } // Finally, the effective value, minus one elements--; } // Update Value public Void update (Int index, long newvalue) { if (index >= elements | | index < 0) { Throw new arrayindexoutofboundsexception (); } arr[index] = newvalue; }}
Test Case:
package com.xingej.algorithm.datastructure.array;import org.junit.before;import org.junit.test; import com.xingej.algorithm.datastructure.array.myarray;/** * Object-Oriented programming, * * That is, the you operate is an object, not a basic data type * * * @author erjun 2017 November 28 Afternoon 9:59:36 */public class myarraytest { private myarray myarray; @Before public void init () { myarray = new myarray (); testinsert (); } // Insert data test @Test public void test () { myarray.insert (&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN) ("---- : \ t " + myarray.geT (0)); } @Test public void Testinsert () { myarray.insert (); myarray.insert (+); myarray.insert (5); myarray.insert (; } ) // Display/Print the contents of the array @Test public void Testdisplay () { myarray.display (); } // Display/Print the contents of the array @Test public Void testgetindexbyvalue () { system.out.println ("--- The index value is: \ T " + myarray.getindexbyvalue ()); } // Deletes the element at the specified position according to the subscript &NBSP;&NBSP;&NBsp; @Test public void testdelete () { &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("---to print elements in an array before deleting------"); Myarray.display (); myarray.delete (5); &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("---to print the elements in the array after deletion------"); myarray.display (); } @Test public void testupdate () { myarray.update (2, 100) ; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("---value after update----: \ t" + myarray.get (2)); }}
The code has been uploaded to Git:
Https://github.com/xej520/xingej-algorithm
An array of Java data structures