Array Learning 1

Source: Internet
Author: User

Package Ch01;public class MyArray {//requires a real array private long [] arr;private int elements;//Number of data elements public MyArray () {arr = new LONG[50];} Public MyArray (int maxsize) {arr = new long[maxsize];} Method of adding data public void Insert (Long value) {arr[elements] = value;elements++;} 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 value according to index public long get (int index) {//Determine if array is out of bounds if (Index < 0 | | Index >= elements) {throw new Arrayindexoutofbou Ndsexception ("Array 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 ArrayIndexOutOfBoundsException ();} else {Arr[index] = NEWV Alue;}}}

  

Array Learning 1

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.