Ordered Array 2

Source: Internet
Author: User

Package Ch01;public class Myorderarray {private long [] arr;private int elements;//array true number public Myorderarray () {arr = new long[50];//the size of the default array is 50}public myorderarray (int maxsize) {arr = new long[maxsize];} method of inserting elements public void Insert (Long value) {int i;for (i=0;i<elements;i++) {if (Arr[i] > value) {break;}} for (int j = elements;j>i;j--) {Arr[j] = arr[j-1];} Arr[i] = value;elements++;} Dichotomy Lookup public int binarysearch (Long value) {int low = 0;int POW = elements;int middle = 0;while (true) {middle = (LOW+POW)/2; If the middle value is the value to find if (arr[middle] = = value) {return middle;} else if (Low > Pow) {return-1;} Else{if (Arr[middle] > value) {pow = middle-1;} Else{low = Middle+1;}}} The method that is displayed public void display () {System.out.print ("["); for (int i=0;i<elements;i++) {System.out.print (arr[i]+ "");} System.out.print ("]"); System.out.println ();} Find data method, find data based on value to find, return value of subscript public int search (Long value) {int i;for (i=0;i<elements;i++) {if (arr[i] = = value) {break;}} if (i = = elements) {return-1;} Else{return i;}} Find data, return values by index pUblic long get (int index) {//Determine if array is out of bounds if (Index < 0 | | Index >= elements) {throw new ArrayIndexOutOfBoundsException ("number Group out of Bounds ");} Else{return arr[index];}} Delete data public void Delete (int index) {if (Index >=elements | | Index < 0) {throw new ArrayIndexOutOfBoundsException (" Array out of Bounds ");} else{for (int i=index;i<elements;i++) {Arr[index] = arr[index+1];} The length of the array--elements--;}} Update data public void change (int index,int newvalue) {if (Index >= elements | | Index < 0) {throw new arrayindexoutofbounds Exception ();} else {Arr[index] = newvalue;}}}

  

Ordered Array 2

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.