Java-static applications (Tool class)

Source: Internet
Author: User

Arraytool. Java File

/** Static application. Each application has the public action function, which can be extracted and encapsulated independently for reuse because ArrayTool does not encapsulate special data, in addition, each method of the array does not use the specific data in ArrayTool. To make the program more rigorous, the methods in ArrayToll can be defined as static, you can call ArrayTool directly with the class name and then send it to others, other people only need to set this file to the classpath path, so that the tool can be used to make the other party clear the functions of this class, you can use the Manual (document comment) to complete * // ** this is a tool class that can conduct CAO group on arrays. This class provides the maximum (minimum), sorting function @ author XX @ version V1.0 */class ArrayTool {/** null parameter constructor, due to the reasons mentioned earlier, avoid occupying too much useless memory by creating objects, declared as private type */private ArrayTool () {}/ ** get the maximum value of the meaningful integer array @ par Am arr accepts an int array @ return max and returns the maximum value */public static int getMax (int [] arr) {int max = 0; for (int I = 0; I <arr. length; I ++) {if (arr [I]> max) max = arr [I];} return max ;} /** get the minimum value of the meaningful integer array @ param arr accept an int array @ return min returns the minimum value of the array */public static int getMin (int [] arr) {int min = 9999; for (int I = 0; I <arr. length; I ++) {if (arr [I] <min) min = arr [I];} return min ;} /** select sorting for int-type arrays (from small to large) @ param arr to accept an int-type array */public st Atic void selectSort (int [] arr) {for (int I = 0; I <arr. length-1; I ++) {for (int j = I + 1; j <arr. length; j ++) {if (arr [I]> arr [j]) {swap (arr, I, j );}}}} /** bubble sort the int array (from small to large) @ param arr to accept an int array */public static void bubbleSort (int [] arr) {for (int x = 0; x <arr. length-1; x ++) {for (int y = 0; y <arr. length-x-1; y ++) {if (arr [y]> arr [y + 1]) {swap (arr, y, y + 1 );}}}} /** switch the position of elements in the array @ param arr array @ param I the coordinate of the position to be exchanged 1 @ param I Change location coordinates 2 */private static void swap (int [] arr, int I, int j) {int temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;}/** print the elements in the array. */Public static void printArray (int [] arr) {System. out. print ("["); for (int I = 0; I <arr. length; I ++) {if (I! = Arr. length-1) System. out. print (arr [I] + ","); elseSystem. out. println (arr [I] + "]") ;}}}

Arraytooldemo File

Class arraytooldemo // test the main function {public static void main (string [] ARGs) {int [] arr = {32, 1, 45, 6, 89,33, 99 }; // declare an array without arraytool. printarray (ARR); // print the array int max = arraytool. getmax (ARR); // test the maximum value of system. out. println ("max =" + max); int min = arraytool. getmin (ARR); // test the minimum value of system. out. println ("min =" + min); // arraytool. printarray (ARR); // print the array // arraytool. selectsort (ARR); // test selection sorting // arraytool. printarray (ARR); // print the sorted array arraytool. printarray (ARR); // print the array arraytool. bubblesort (ARR); // test the Bubble Sorting arraytool. printarray (ARR); // print the sorted array }}

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.