Algorithm design for ascending sorting of Java collection and array

Source: Internet
Author: User

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

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.