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