Data Structure-array

Source: Internet
Author: User

Data Structure-array is the most widely used data storage structure. It is embedded into programming languages. Because arrays are easy to understand, they are used as the starting point for introducing data structures and displaying the relationships between object-oriented programming and data structures. Unordered array: creates an array class and calls the array class to operate the array. // The publicclass HighArray {privatelong [] a; privateintnElem; public HighArray (int size) {a = newlong [size] Through object-oriented operations; nElem = 0;} // insert data into the array publicvoid setA (long value) {a [nElem] = value; nElem ++ ;} // query data by condition publicboolean Seachkey (long value) {boolean s = false; for (int I = 0; I <nElem; I ++) {if (a [I] = value) {s = true; break;} else {s = false ;}} return s ;} // delete the specified data publicboolean delete (long value ){ Boolean ss = false; for (int I = 0; I <nElem; I ++) {if (a [I] = value) {for (int k = I; k <nElem; k ++) {a [k] = a [k + 1];} ss = true; break ;}} nElem --; return ss ;} // output all data publicvoid display () {for (int I = 0; I <nElem; I ++) {System. out. print (a [I] + "") ;}} publicclass HigArrayApp {publicstaticvoid main (String args []) {int maxSize = 100; HighArray hh; // reference HighArray class hh = new HighArray (maxSize); // create the HighArray object hh. SetA (99); hh. setA (77); hh. setA (88); hh. setA (66); hh. setA (55); hh. setA (44); hh. setA (33); hh. setA (22); hh. setA (11); hh. setA (26); // output array hh. display (); // query specific data hh. seachkey (66); if (hh. seachkey (66) {System. out. println ("found:" + 66);} else {System. out. println ("not found:" + 66);} // delete specific data hh. delete (26); hh. display () ;}} ordered array: the binary search method provides the advantages of an ordered array when binary search is used. For example, assume that you only have a number in 100 (or n). You should start with 50 (or n/2) and assume that the number of guesses is greater than 50, you can guess the number from the center between-, and so on, but the number will be much less. This is the binary search method. Example: publicclass OrdHigh {privatelong [] d; // reference array a privateintnElems; // number of records in public OrdHigh (int max) {d = newlong [max]; // create an array nElems = 0;} publicint size () {// return the nElems value returnnElems ;} /* use the binary query method to query specific data */publicint find (long seachkey) {int lowerBound = 0; // The subscript int upperBound = nElems-1 that stores the minimum value; // store the maximum subscript int curIn; // store the median subscript while (true) {curIn = (lowerBound + upperBound)/2; // obtain the subscript of the intermediate value if (d [curIn] = s Eachkey) {// determine whether the center value is equal to the specific data return curIn;} elseif (lowerBound> upperBound) {returnnElems; // when not found} else {if (d [curIn]> seachkey) {upperBound = curIn-1; // use the subscript-1 of the intermediate value as the subscript of the maximum value} else {lowerBound = curIn + 1; // use the subscript + 1 of the intermediate value as the subscript of the minimum value }}/ * store the value in an ordered array */publicvoid insert (long value) {// store the array in order for (int j = 0; j <nElems; j ++) {// If the saved value is smaller than the original value, return, if it is large, move it to a value smaller than it. if (d [j]> value) {break;} else {for (int I = nElems; I> j; I --) {d [I] = d [I-1];} d [j] = value;} nElems ++ ;} // delete the specified data publicboolean delete (long value) {int j = find (value); if (j = nElems) {returnfalse; // No specified value found} else {for (int I = j; I <nElems; I ++) {d [I] = d [I + 1];} nElems --; returntrue ;}// query all data publicvoid display () {for (int j = 0; j <nElems; j ++) {System. out. print (d [j] + "") ;}} publicclass OrdHighApp {publicstaticvoid main (String args []) {int m AxSize = 100; OrdHigh ss; ss = new OrdHigh (maxSize); ss. insert (77); ss. insert (88); ss. insert (99); ss. insert (44); ss. insert (55); ss. insert (33); ss. insert (66); ss. insert (22); ss. insert (11); ss. display (); www.2cto.com if (ss. find (55 )! = Ss. size () {System. out. println ("found:" + 55);} else {System. out. println ("not found:" + 55);} ss. delete (77); ss. display ();}}

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.