The procedure in the previous essay, after thinking, found that using too many static functions and not incorporating object-oriented ideas made a change:
1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 Importjava.util.List;4 ImportJava.util.Scanner;5 6 7 Public classClasstest {8 9List<string> number=NewArraylist<string>();TenList<string> word=NewArraylist<string>(); One A classtest () {}; - voidCount (list<string>l) { - for(intI=0;i<l.size (); i++){ the if(Isnumber (L.get (i))) { - Number.add (L.get (i)); -}ElseWord.add (L.get (i)); - } +System.out.println ("Numbercount:" +number.size ()); -System.out.println ("WORDCOUNT:" +word.size ()); +}//count the number of strings and numbers A at - BooleanIsnumber (String a) { - Try { -Integer.parseint (a);//numeric string conversion int type number "123"->123 - return true; -}Catch(Exception e) { in return false; - } to}//determine whether a number + - the * Public Static voidMain (string[] args) { $ classtest s=new classtest (); Panax NotoginsengSYSTEM.OUT.PRINTLN ("Please input the string"); -Scanner get=NewScanner (system.in); theString str=get.nextline (); +System.out.println ("string is" +str);//Keyboard Get string A theList<string> list=NewArraylist<string> ();//problem? + -string[] Text = Str.split (""); $ for(inti=0;i<text.length;i++){ $ List.add (Text[i]); -}//Deposit List - the - S.count (list);Wuyi the //Collections.sort Sort - Collections.sort (s.number); Wu Collections.sort (s.word); -System.out.println ("number sort:" + s.number); AboutSystem.out.println ("word Sort:" + S.word); $ } - -}
The bold code is for modifying the added part. This completes the encapsulation of a program.
An object-oriented approach to the "list feature in Java" program for the last "simple character and numeric statistics on strings"