An array of integers in ascending order (with 20 integers) is required to enter an integer from the keyboard, insert the number into the array, and keep it in ascending order, outputting the correctness of the numerical validator for the array. package test_array;import java.util.arraylist;import java.util.arrays;import Java.util.collection;import java.util.scanner;public class arraytest {public static void main (String[] args) {arraytest at = new arraytest (); At.sortArray ();// In order to embody the encapsulation characteristics of the Java language, we will use the main method of execution outside of the}object sortarray () method () { /** Here we first create an array and initialize * then speak the array into the collection after adding the number of input from the keyboard * finally convert the collection to an array * and then call the Arrays.sort () method to output in foreach **/int[] array = new int[]{-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9};//The generic mechanism is added here, the program security is higher collection< Integer> arraylist = new arraylist<integer> (); System.out.println ("Old array ' s length:" +array.length); for (int i=0;i<array.length;i++) { System.out.print (" " +array[i]); Arraylist.add (New integer (Array[i]));} System.out.print ("\ n" + "Please inPut a number: "); Arraylist.add (New integer (Input ())); System.out.println ("Current array ' Length:" +arraylist.size ()); Object[] now=arraylist.toarray (); Arrays.sort (now), for (Object currentarray:now) {System.out.print (" " +currentarray);} Return arraylist;} Static int input () {int input = (New scanner (system.in)). Nextint (); return input;}}
This article is from the "10891776" blog, please be sure to keep this source http://10901776.blog.51cto.com/10891776/1731193
Algorithm design for ascending sorting of Java collection and array