How to implement ordered Array by java
This article mainly introduces "how to achieve ordered arrays in java". In daily operation, I believe many people have doubts about how to achieve ordered arrays in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve ordered arrays in java". Next, please follow the editor to study!
Package com.clean.array;public class OrderArray {private long [] a; private int nElems; public OrderArray (int max) {a = new long [max]; nElems = 0;} public int size () {return nElems;} public int find (long key) {int lowBound = 0; int highBound = nElems-1; int curIn While (true) {curIn = (lowBound + highBound) / 2; if (a [curin] = key) {return curIn;} else if (lowBound > highBound) {return nElems;} else {if (a [curin])
< key) { lowBound = curIn + 1; } else { highBound = curIn -1; } } } } public void insert(long value) { int j; for(j = 0; j < nElems; j ++) { if(a[j] >Value) {break;}} for (int k = nElems; k > j; k--) {a [k] = a [k-1];} a [j] = value; nElems + +;} public boolean delete (long value) {int j = find (value) If (j = = nElems) {return false;} else {for (int k = j; k < nElems; k + +) {a [k] = a [k + 1];} nElems -; return true;}} public void disPlay () {for (int j = 0; j < nElems) J + +) {System.out.print (a [j] + ");} System.out.println ();}} package com.clean.array;public class OrderApp {public static void main (String [] args) {int max = 100; OrderArray orderArray = new OrderArray (max); orderArray.insert (200); orderArray.insert (100); orderArray.insert (500) OrderArray.insert (300); orderArray.insert (10); orderArray.insert (20); orderArray.insert (11); orderArray.disPlay (); int key = 300; if (orderArray.find (key)! = orderArray.size ()) {System.out.println ("find:" + key) } else {System.out.println ("not find:" + key);} orderArray.disPlay (); orderArray.delete (200); orderArray.delete (10); orderArray.delete (11); orderArray.disPlay ();}} this ends the study of "how java implements ordered arrays", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!