Array, String,stringbuilder, collection

Source: Internet
Author: User

1. Definition of arrays, traversal, and methods

Definition: int arr[] = {1,2,3,4,5};// static initialization
int arr1[]= New int[5];//dynamic initialization
To iterate over an array:

for (int i = 0; i < arr.length; i++) {System.out.println (arr[i]);}

The methods and properties of the

array :length length;

2
Definition:
//mode 1
//string (string original): Encapsulates string data into a String object
String s1 = new String ("Hello");
Mode 2
//string (char[] value): Encapsulates the data of a character array into a string object
char[] chs = {' h ', ' e ', ' l ', ' l ', ' o '};
String s2 = new string (CHS);
//Mode 3
//string (char[] value, int index, int count): Encapsulates part of the data in a character array into a string object
//string s3 = new String (chs,0, Chs.length length);
String s3 = new String (chs,1,3);
//Mode 4
string s4 = "Hello";

Traversal:
//gets the length of the string using the length () method
for (intx=0; x<s.length (); x + +) {
System.out.println (S.charat (x));
}
Method:
Judging function method

? Boolean equals (Object obj): Compares the contents of a string? Boolean Equalsignorecase (String str): Compares the contents of a string for the same, ignoring case? Boolean StartsWith (String str): Determines whether the string object begins with the specified str? boolean endsWith (String str): Determines whether the string object ends with the specified str

? Get functional methods

? int Length (): Gets the length of the string, in fact, the number of characters? Char charAt (int index): Gets the character at the specified index? int indexOf (String str): Gets the index of the first occurrence of STR in a string object. No return-1? string substring (int start): Intercepts a string from start to end of string? string substring (int start,int end): Intercepts the string from start to end, including start, excluding end

? Conversion Function method

? char[] ToCharArray (): Converts a string to a character array? String toLowerCase (): Converts a string to a lowercase string? String toUpperCase (): Converts a string to an uppercase string

? remove whitespace and split function methods

? char[] ToCharArray (): Converts a string to a character array? String toLowerCase (): Converts a string to a lowercase string? String toUpperCase (): Converts a string to an uppercase string

  

3.definition, traversal, and method of StringBuilder
Defined:
Creating objects
StringBuilder builder = new StringBuilder ();
//method of calling append directly

Builder.append ("Hello"); Builder.append ("World"); Builder.append (true); Builder.append (100);

Chained programming

Builder.append ("Hello"). Append ("World"). Append (True). Append (100); System.out.println ("Builder:" +builder);

  

Traversal://Fangfali Creates a StringBuilder type inversion and then ToString becomes a string type
Publicstatic string Myreverse (string s) {
String--StringBuilder--reverse ()--string
string s = new string ("Hello");//Defining a string string
StringBuilder builder= New StringBuilder (s);//change string string to StringBuilder object
Builder.reverse ();//Call Reverse Inversion method
string result = Builder.tostring ();//Turn StringBuilder object into string string
Return result;
}

Method:
Add Features
Public StringBuilder Append (any type): Adds data and returns its own object
such as: Builder.append (TRUE);
? reversal function
? public StringBuilder Reverse ()
such as: Builder.reverse ();


The string turns into StringBuilder:

StringBuilder constructs a method, or a Append method such as: string s = new string ("Hello");//defines a string string StringBuilder builder= new StringBuilder (s);//change string string to StringBuilder object or: StringBuilder builder = new StringBuilder (s);

  StringBuilder turns into a string:

The method of the StringBuilder class public string toString () such as: string result = Builder.tostring ();

4. definition, traversal, and method of the collection

Defined:
To create a collection object
Arraylist<string>list = new arraylist<string> ();

Traverse:

3. Iterate through the collection, fill the left side of the element with a length of less than 4 with string 8, and in the console print the output modify all elements in the collection for (int i = 0; i < list.size (); i++) {if (List.get (i). Length () <4) {L Ist.set (i, 8 + list.get (i));}} SYSTEM.OUT.PRINTLN (list);

Method:
? Add elements
? public boolean Add (E): add element
such as: List.add ("Hello");
public void Add (int index,e Element): Adds an element at the specified index
such as: List.add (1, "Android");
? Get elements
? Public E get (int index): Returns the element at the specified index
? Set length
public int size (): Returns the number of elements in the collection
? Delete an Element
? public boolean remove (Object O): Deletes the specified element, returning whether the deletion succeeded
Public E Remove (int index): Deletes the element at the specified index and returns the deleted element
? modifying elements
? public E Set (int index,e element): Modifies the element at the specified index, returning the modified element

Array, String,stringbuilder, collection

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.