Demand:
Keyboard input multiple data, enter 0 o'clock to get results, output the largest one
Analysis:
A: Create A Keyboard entry
B: Because the number of data is uncertain, receive with the collection
C: Put the keyboard input data into the set
D: Because we didn't learn the sort of collection, we had to turn the set into a group
Public object[] ToArray () returns an array that contains all the elements in this list, in the appropriate order (from first to last element).
Public <T> t[] ToArray (t[] a) returns an array containing all the elements in this list in the appropriate order (from first to last); The run-time type of the returned array is the run-time type of the specified array.
E: Sorting data for an array
F: Output index is length-1 that data
G: Print, I want to print out the acquired collection again
A: Create a method to stitch the array to a string
return type: String
Parameter list: Integer XX
B: Refer to this method when printing
1 Packagecn_jdk5new;2 3 Importjava.awt.List;4 Importjava.util.ArrayList;5 Importjava.util.Arrays;6 ImportJava.util.Scanner;7 8 Public classArrayLIstDemo2 {9 Ten One Public Static voidMain (string[] args) { A //Create keyboard Entry -Scanner sc =NewScanner (system.in); - //Create a collection theArrayList <Integer> list =NewArraylist<integer>(); - - //Put the keyboard input data into the set - while(true){ +System.out.println ("Please enter data:"); - inti =sc.nextint (); + A if(I! = 0){ at List.add (i); - } - Else{ - Break; - } - } in //create an array with the same length as the list - //to convert a collection to an array to //Public <T> t[] ToArray (t[] a) +Integer[] in =Newinteger[list.size ()]; - List.toarray (in); the * //sort data within an array $ //Arrays: This class contains various methods for manipulating arrays, such as sorting and searching. Panax Notoginseng Arrays.sort (in); - theSYSTEM.OUT.PRINTLN ("Array is:" +arraytostring (in) + "\ t maximum value is" +in[in.length-1]); + } A //Create a method to stitch the array the //array to string concatenation + Public StaticString arraytostring (integer[] in) { - //to Splice, create a string buffer $StringBuffer SB =NewStringBuffer (); $ //defining the beginning of a buffer -Sb.append ("["); - //iterating through an array, stitching it into a character buffer the for(intx = 0; × < in.length; x + +){ - if(X < In.length-1){WuyiSb.append (in[x]+ ","); the } - Else{ Wu sb.append (in[x]); - } About } $ //defining the end of a buffer -Sb.append ("]"); - //the string buffer is converted to a string and returned - returnsb.tostring (); A } + } the -
Java 16-16 Keyboard input data to find the maximum value