Dark Horse Programmer---java Foundation--a comprehensive test question for sets, arrays, strings

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

This problem synthesizes the collection, the array, the string, the string buffer, the automatic packing and so on the knowledge point, you did this question to be done, Java Foundation You also learned the almost.


Problem:

Customize a string that consists of lowercase letters to count the occurrences of each letter.

For example: ABC, the output is: A (1) B (1) C (1)


Code implementation and parsing

Package Cn.itcast_test;import Java.util.scanner;import Java.util.set;import java.util.treemap;/* Customize a string that consists of lowercase letters to count the occurrences of each letter. * For example: ABC, output result is: A (1) B (1) C (1) *  * analysis. * 1, need to define a keyboard entry function * 2, need to use a map collection to receive, in order to be able to sort alphabetically here with TreeMap collection. * Key: Character * Value: Integer * 3, convert string to character array. * 4, iterate through the character array, get each character * 5, get 4 of the resulting character as a key to the collection to find the value, see the return value * NULL: The key does not exist, the character as a key, 1 as a value to store. * NOT NULL: Indicates that the key exists, add a value of 1, then re-store the key and value * 6, define the string buffer variable * 7, iterate through the collection, get the key and value, splice * 8 as required, and convert the string buffer to string output. */public class Sumstring {public static void main (string[] args) {//keyboard input Scanner sc=new Scanner (system.in); System.out.println ("Please enter a string consisting of lowercase letters"); String S=sc.nextline ();
Defines a TreeMap collection object Treemap<character,integer> tree=new treemap<character,integer> ();//Converts a string into an array. Char[] Chs=s.tochararray ();
Iterate through the array and get to the set to find the value for (Character key:chs) {Integer value=tree.get (key), if (value==null) {tree.put (key, 1);} Else{value++;tree.put (key, value);}}
Define the string buffer StringBuilder sb=new StringBuilder ();//Iterate through the collection and Splice set<character> Set=tree.keyset () as required; for ( Character key:set) {int value=tree.get (key), Sb.append (key). Append ("("). Append (Value). Append (")");}
Convert the Buffer object to a string and output System.out.println (sb.tostring ());}} /** * Written in the back * This question synthesizes the following knowledge points * 1, set TreeMap * 2, keyboard input * 3, string and string buffer object conversion * 4, array and collection of traversal (precedence with enhanced for loop) * 5, String buffer splicing function * 6,jdk5 after C Haracter and integer auto-boxing function * If you can easily make this problem, you have mastered the above knowledge points, as long as more practice, you will be proficient in the use of these knowledge points. */



Dark Horse Programmer---java Foundation--a comprehensive test question for sets, arrays, strings

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.