Java Basic Knowledge Enhancement Collection Framework Note 36:list keyboard input multiple data in the console output maximum value

Source: Internet
Author: User

1. Keyboard input multiple data, end with 0, require the console output of the maximum value in this multiple data

Analysis:
  • Create keyboard Entry data Objects
•    keyboard Input Multiple data, we do not know how many, so with a collection of storage
•    End With 0, this simple, as long as the keyboard input data is 0, I will not continue to enter data
  • turn the collection into a group
  • sorting an array
•    Gets the value of the largest index in the array

2. Code implementation:

11. Packagecn.itcast_03;2 3 Importjava.util.ArrayList;4 Importjava.util.Arrays;5 ImportJava.util.Scanner;6 7 /*8 * Keyboard input multiple data, end with 0, require the console output of the maximum value in this multiple data9  * Ten * Analysis: One * A: Create A Keyboard Entry data Object A * B: Keyboard input Multiple data, we do not know how many, so with a collection of storage - * C: End with 0, this simple, as long as the keyboard input data is 0, I will not continue to enter data - * D: Turn the set into a group the * E: Sorting an array - * F: Gets the value of the largest index in the array -  */ -  Public classArraylistdemo { +      Public Static voidMain (string[] args) { -         //Creating a Keyboard entry data object +Scanner sc =NewScanner (system.in); A  at         //keyboard input Multiple data, we do not know how many, so with a collection of storage -arraylist<integer> array =NewArraylist<integer>(); -  -         //End With 0, this simple, as long as the keyboard input data is 0, I will not continue to enter data -          while(true) { -System.out.println ("Please enter data:"); in             intNumber =sc.nextint (); -             if(Number! = 0) { to Array.add (number); +}Else { -                  Break; the             } *         } $ Panax Notoginseng         //Turn a collection into an array -         //Public <T> t[] ToArray (t[] a) theinteger[] I =Newinteger[array.size ()]; +         //integer[] II = Array.toarray (i); A Array.toarray (i); the         //System.out.println (i); +         //System.out.println (ii); -  $         //sort an array $         //Public static void sort (object[] a) - Arrays.sort (i); -  the         //gets the value of the largest index in the array -SYSTEM.OUT.PRINTLN ("Array is:" + arraytostring (i) + "maximum value is:"Wuyi+ i[i.length-1]); the     } -  Wu      Public StaticString arraytostring (integer[] i) { -StringBuilder SB =NewStringBuilder (); About  $Sb.append ("["); -          for(intx = 0; x < i.length; X + +) { -             if(x = = I.length-1) { - sb.append (i[x]); A}Else { +Sb.append (I[x]). Append (","); the             } -         } $Sb.append ("]"); the  the         returnsb.tostring (); the     } the}

Run the results as follows:

Java Basic Knowledge Enhancement Collection Framework Note 36:list keyboard input multiple data in the console output maximum value

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.