Java strings, packets, arrays and hollow squares, examples of diamonds

Source: Internet
Author: User

One, array: multiple pairs of the same type
Reference types: All classes, interfaces, arrays,
Int[] ints (variable name) = new Int[3]
NEW: Refers to re-opening an area in memory space

String S1 ="ABC"; String S2="ABC"; System. out. println (s1==S2); String S3=NewString ("ABC"); String S4=NewString ("ABC"); System. out. println (s3==S4); System. out. println (S3.equals (S4)); 

Analysis:

String S1 = "abc";
String s2 = "abc";
System.out.println (S1==S2);//After the TRUE,S1 definition is output, S2 will look for the stored address in the constant pool, s1==s2 to see if it points to the same memory storage address

string S1 = new String ("abc");

String s2 = new String ("abc");

System.out.println (S1==S2);//Output False This is the definition of a new array in a constant pool, S2 is also defined as a new, that is, in the constant pool of two identical arrays, S1==s2 point to the storage address is different, so is false

Second, Packing Type: integer.parseint ()

int 、、、 Integer
Short 、、、 Short
Long 、、、 Long
Float 、、、 Float
BYTE 、、、、 byte

System. out. println (Integer.min_value); System. out. println (Integer.max_value); System. out. println (Byte.min_value); System. out. println (Byte.max_value); System. out. println (Long.min_value); System. out. println (Long.max_value); System. out. println (Short.min_value); System. out. println (Short.max_value); System. out. println (Float.min_value); System. out. println (Float.max_value); System. out. println (Double.min_value); System. out. println (Double.max_value); System. out. println (Float.parsefloat ("12.34"));

Analysis: The maximum minimum value for each data type, respectively

Third, string Processing

String str = "A New World a new Strat";
System.out.println (Str.length ());//Returns the length of the entire string
System.out.println (Str.trim ());//Remove the length of the space on both sides of the string
System.out.println (Str.trim (). Length ());//returns the entire string
System.out.println (Str.charat (3));//Remove the characters in the string that make the index position
System.out.println (Str.contains ("abc"));//Determine if a string contains another string
. StartsWith () Determines whether a string begins with this
System.out.println (str.replace (' A ', ' B ')); change a in the string to B
.
. toUpperCase () Change the case of a string (uppercase)
. toLowerCase () lowercase
. ValueOf () Converts a number to a string output tostring ()
where the. indexOf ("new") string first appears
The last occurrence of the. LastIndexOf ("new") position, if not returned-1
. SUBSTRING (5) starts with the 5th character intercept including the 5th
. SUBSTRING (5,8) starts with the first digit, ends with the second digit, does not contain a second number

str.length (); Str.trim (); Str.charat (int  i); Str.contains (charsequence s); Str.startswith ( string s); Str.endswith (string s); replace(charchar  n); replace (Charsequence O, Charsequence n); split (string s); toUpperCase (); toLowerCase (); valueOf (any args); Str.indexof (string s); Str.lastindexof (String s); str.substring (int  i); str.substring (intint b);

String str = "A team like a warrior can only seize the opportunity and play three games, but the jazz still can't find a way to deal with the warrior".

1, write code to find the keyword "team", "opportunity" where the string str index position, find the string of the second "warrior" position, and output on the console
2, defines the int variable m, the value of all the index values in the first question and
3, the contents of the output m as char type on the console
4, write code implementation will str string with "," to split the array, and output the value of the index value of 4
5, write code implementation will "jazz" in the STR string instead of "warrior", "Warrior" with "jazz" instead, and output the result to the console (disable replace method)
6. Write code to take a portion of the STR string to print on the console this sentence: Warriors seize the opportunity to find a way to cope
7, write a piece of code, you can take out any QQ e-mail address in the QQ number
String qqemail = "[email protected]";

intA=0; intb=0; intC=0; intm=0; String Str="A team like a warrior can only seize the opportunity and play three games, but the jazz still can't find a way to deal with the warrior."; b=str.indexof ("Team"); A=str.indexof ("Opportunities"); C=str.lastindexof ("Warriors"); M=a+b+C; System. out. println (Char) m);

System. out. println (Str.split (",")[4]); String[] Newstr= Str.split ("");  for(intI=0; i<newstr.length;i++){            if("Yong". Equals (Newstr[i])) {Newstr[i]="Spyker"; }Else if("Spyker". Equals (Newstr[i])) {Newstr[i]="Yong"; } System. out. Print (Newstr[i]); }

System. out. println (str.substring (1,3));        System. out. println (str.substring (+));        System. out. println (str.substring (PNs));        System. Out. println (str.substring (.)); 

" [email protected] " ; System. out. println (qqemail.substring (0,8));

8, use for and if to print a hollow square
9, use a For loop to print a diamond
10. Use a For loop to print a hollow diamond (optional title)

Package AAA; Public classText { Public Static voidMain (string[] args) {intn =5;  for(inti =0; I < n; i++) {             for(intj =0; J < N; J + +) {                if(i==0|| i==4) {System. out. Print ("* "); } Else {                    if(j==0|| j==4) {System. out. Print ("* "); } Else{System. out. Print ("  "); }}} System. out. println (); }    }}

     for(intI=4; I>0; i--){               for(intj=0;j<8; j + +){                    if(j>i&&j<8-i) {System. out. Print ("*"); }Else{System. out. Print (" "); }}system. out. println (); }         for(intI=0;i<4; i++){             for(intj=0;j<8; j + +){            if(J >i&&j<8-i) {System. out. Print ("*"); }Else{System. out. Print (" "); }}system. out. println (); }

Analysis: The first is to determine the length and width are 7 square, divided into two parts, the upper part is I is not see the space portion, with the increase in the *, I smaller, so for self-reduction, J is the number of asterisks, the lower half is I with j increase, I gradually increase, the number of asterisks is also getting smaller.

Java strings, packets, arrays and hollow squares, examples of diamonds

Related Article

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.