Java-Array application

Source: Internet
Author: User

I feel that sometimes Java is really awesome !!!

I think I can use Java...

I will continue to update this article. I think that if you want to use Java in ACM, you must make good use of arrays, so it is necessary to learn arrays well,

Step 1: I learned how to dynamically generate arrays and sort them more important,

Code:

Package com; import Java. util. arrays; import Java. util. dependencies; // import Java. util. arrays; public class demobinarysearch {public static void main (string [] ARGs) {custom input = new role (system. in); int N; while (input. hasnext () {n = input. nextint (); int [] A = new int [N]; // you have to say this is really amazing. You can also store variables when applying for memory .... for (INT I = 0; I <n; I ++) {A [I] = input. nextint ();} // arrays. sort (A, 0,. length); // The arrays of the N * log (n) algorithm. sort (a); // these two sorting methods are good. The first one is to sort as many elements as possible, in this way, you have to dynamically generate the number you need. // the second one is like the above one, indicating the start position and the last position. for (INT I = 0; I <n; I ++) {system. out. print (A [I]); system. out. print ("");} system. out. println ();}}}

Another useful binarysearch () ---- Binary Search

Package com; import Java. util. arrays; import Java. util. dependencies; // import Java. util. arrays; public class demobinarysearch {public static void main (string [] ARGs) {custom input = new role (system. in); int N; while (input. hasnext () {n = input. nextint (); int [] A = new int [N]; // you have to say this is really amazing. You can also store variables when applying for memory .... for (INT I = 0; I <n; I ++) {A [I] = input. nextint ();} // arrays. sort (A, 0,. length); // The arrays of the N * log (n) algorithm. sort (a); // these two sorting methods are good. The first one is to sort as many elements as possible, in this way, you have to dynamically generate the number you need. // the second one is like the above one, indicating the start position and the last position. for (INT I = 0; I <n; I ++) {system. out. print (A [I]); system. out. print ("");} system. out. println (); // int idx = arrays. binarysearch (A, 0,. length, 5); // Of course, the prerequisite for this binary is that the order has been sorted, int idx = arrays. binarysearch (A, 5); // binary search, this is also good. I said that Java has written all the things, so I am embarrassed to do so, we don't need to write anything. system. out. println ("The idx =" + (idx + 1 ));}}}

The following describes the fill and copy functions:

Package com; import Java. util. arrays; import Java. util. optional; public class demoarrays {public static int N; public static void print (INT [] A) {for (INT I = 0; I <. length; I ++) {system. out. print (A [I] + "");} n ++; system. out. println ();} public static void main (string [] ARGs) {int [] B; int [] C; input = new partition (system. in); While (input. hasnext () {n = input. nextint (); int [] A = new int [N]; arrays. Fill (A, 1); // The fill method is to directly fill all the content of the array into the same content B = arrays. copyof (A, 20); // copy the content of the array to array c = arrays. copyofrange (A, 0, 10); // copy the content 0-10 from array A to C; print (); // if you do not want to create an object to directly use the method in the class, you have to apply for a static method, and the static method can only use static variables. print (B); print (c); system. out. println (n); // here n is the N after execution of N ++. so we should know that static methods can only reference static constants, which is equivalent to global variables in C ++ !!! For (INT I = 0; I <n; I ++) {A [I] = input. nextint ();}}}}

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.