Detailed description of how to count the number of characters in Java and how to calculate non-identical characters in reverse order

Source: Internet
Author: User

Copy codeThe Code is as follows: import java. util. ArrayList;
Import java. util. List;
Public class Test2 {
/**
* @ Param args
*/
Public static void main (String [] args ){
String src = "a B c d e B C ";
// Replace Spaces
Src = src. replaceAll ("","");

System. out. println ("remove space character string:" + src );
List <Character> list = new ArrayList <Character> ();
Int [] bb = new int [256];
Char [] cs = src. toCharArray ();

// Reverse Order
Int mid = cs. length/2;
Int idx = cs. length-1;
For (int I = 0; I <mid; I ++ ){
Char tmp = cs [I];
Cs [I] = cs [idx];
Cs [idx] = tmp;
Idx --;
}
// Statistics and filter the same
For (char c: cs ){
If (bb [c] <1 ){
List. add (c );
}
Bb [c] = bb [c] + 1;
}
System. out. println ();
For (int I = 0; I <list. size (); I ++ ){
System. out. print (list. get (I ));
}
System. out. println ();

For (int I = 0; I <list. size (); I ++ ){
Char c = list. get (I );

System. out. println (c + "" + bb [c] + "");
}

}
}

String a = "abcd, efg ";
String B = ") (* & ^ % $ #@! [] {},. //;: '? <> ";
The requirement is to determine whether a character in String a appears in String B. The higher the efficiency, the better.

* Check whether some characters appear in another string.Copy codeThe Code is as follows :*
* @ Author Java (java2000.net)
*/
Public class Test {
/**
* @ Param args
*/
Public static void main (String [] args ){
String a = "abcd, efg ";
String B = ") (* & ^ % $ #@! [] {},. //;: '? <> ";
Byte [] bb = new byte [2, 256];
Char [] cs = B. toCharArray ();
For (char c: cs ){
Bb [c] = 1;
}
Cs = a. toCharArray ();
For (char c: cs ){
If (bb [c] = 1 ){
System. out. println (c );
}
}
}
}

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.