Dark Horse programmer--javaapi

Source: Internet
Author: User
Tags addall

--java Training, Android training, iOS training,. NET training, look forward to communicating with you! ——-

first, the String class
Features: Once initialized, it cannot be changed.

The string class copies the Equals method in the object class, which is used to determine whether a string is the same.
The string class is useful for describing a string thing, so it provides several ways to manipulate a string:

1. Get
1.1 The number of characters contained in the string, that is, the length of the string
int length ();
1.2 Get a character from a position based on location
char charAt (int index);
1.3 Gets the character in the string position according to the character
int indexOf (int ch); Returns the position of CH for the first time it appears in a string
int indexOf (int ch,int fromIndex); index starting from FromIndex
int indexOf (String str);
int indexOf (String str, int fromIndex);

2. Judgment
2.1 Whether a character is contained in a string
Boolean contains (str);
2.2 Whether there is content in the string
Boolean isEmpty (); The principle is to determine whether the length of the string is 0
2.3 Whether the string starts with a set content
Boolean startsWith (str);
2.4 Whether the string ends with a development element
Boolaen EndsWith (str);
2.5 Determining if string elements are the same
Boolean equals (str);
2.6 Judging whether the content is the same, ignoring the case
Boolean equalsgnorecase ();

3. Conversion
3.1 Converting a character array into a string
Constructor: String (char[]);
String (Char[],offset,count); Convert part into a string
static method: Static String copyvalueof (char[]);
Static String copyvalueof (Char[],int offset,int count);
3.2 Converting character wear into a character array
Char ToCharArray ();
3.3 Converting a byte array into a string
String (byte[]);
String (Byte[],offset,count);
3.4 Converting a string into a byte array
Byte[] GetBytes ();
3.5 Converting a base data type to a string
static String valueOf (int);
Static String valueOf (double);

4. Replace
String replace (Oldchar, Newchar);
5. Cutting
String[] Split (regex);
6. String
Get part of a string: string substring (begin);
String substring (begin,end);
7. Conversion
7.1 Convert a string to uppercase or lowercase
String toUpperCase ();
String toLowerCase ();
7.2 Remove multiple spaces at both ends of a string
String trim ();
7.3 Comparison of two strings in natural order
int CompareTo (String);

Second, StringBuffer

is a character buffer, which is a container that can change the data.

1. Increase Storage
StringBuffer append (); Add the specified data to the end of the existing data
StringBuffer Insert (index, data); Insert data into the specified index position
2. Delete
StringBuffer Delete (start,end) deletes buffer data. Contains start, does not contain end
StringBuffer Deletecharat (index); Delete the character at the specified position
3. Get
char charAt (int index);
int indexOf (String str);
int LastIndex (String str);
int length ();
String substring (int start, int end);
4. Modification
StringBuffer replace (start,end,string);
void Setcharat (int index, char ch);
5. Modification
StringBuffer reverse ();
6. Put the buffer data in the specified array
void GetChars (int srcbegin, int srcend,char[] Dst,int dstbegin)

Where StringBuffer threads are synchronized, StringBuilder threads are unsynchronized.

Iii. Framework

Collection:
|–list: Ordered (the order in which elements are stored in the collection is consistent with the order in which they are taken), and the elements are indexed. Elements can be duplicated.
|–set: Unordered (the deposit and fetch order may be inconsistent), cannot store duplicate elements. Element uniqueness must be guaranteed.
1, add:
Add (object): Adds an element
AddAll (Collection): Adds all the elements in a collection.
2, Delete:
Clear (): Removes all elements from the collection, emptying the collection.
Remove (obj): Deletes the specified object from the collection. Note: The length of the collection will change if the deletion succeeds.
RemoveAll (collection): deletes some elements. Some elements are consistent with incoming collection.
3, Judge:
Boolean contains (obj): whether the specified element is contained in the collection.
Boolean Containsall (Collection): Whether the collection contains the specified number of elements.
Boolean isEmpty (): whether there are elements in the collection.
4, Get:
int size (): There are several elements in the collection.
5, take the intersection:
Boolean Retainall (Collection): The same element in the current collection that is persisted and specified in the collection. Returns flase if the two collection elements are the same, or true if Retainall modifies the current collection.
6, gets all the elements in the collection:
Iterator Iterator (): iterator
7, turn the collection into an array:
ToArray ();
The list itself is a sub-interface of the collection interface and has all the collection methods. Now learning the common method peculiar to list system, the method of lookup finds that the unique method of list has index, which is the most characteristic of this set.
List: Ordered (the order in which elements are stored in the collection is consistent with the order in which they are taken), and the elements are indexed. Elements can be duplicated.
|–arraylist: The underlying data structure is an array, the thread is out of sync, the ArrayList replaces the vector, and the query element is fast.
|–linkedlist: The underlying data structure is linked list, the thread is not synchronized, adding or deleting elements is very fast.
|–vector: The underlying data structure is an array, thread synchronization, Vector regardless of query and additions and deletions are huge slow.
1, add:
Add (index,element): Inserts an element at the specified index bit.
AddAll (index,collection): Inserts a bunch of elements at the specified index bit.
2, Delete:
Remove (Index): Deletes the element at the specified index bit. Returns the element that was deleted.
3, Get:
Object Get (Index): Gets the specified element by index.
int indexOf (obj): Gets the index bit of the first occurrence of the specified element if the element does not exist return-1;
So, by 1, you can tell if an element exists.
int lastIndexOf (Object o): Reverse index Specifies the position of the element.
List sublist (start,end): Gets the child list.
4, modify:
Object Set (index,element): Modifies elements of the specified index bit.
5, get all the elements:
Listiterator Listiterator (): A list collection-specific iterator.
The list collection supports adding, deleting, changing, and checking elements.
The list collection is because the corner label has its own way of acquiring elements: traversal.
for (int x=0; x

Set keySet = map.keySet();Iterator it = keySet.iterator();while(it.hasNext()) {Object key = it.next();Object value = map.get(key);System.out.println(key+":"+value);}

The way to take out all the elements in the map collection two: The EntrySet () method.

set  entryset = Map.entryset  ()  Iterator it = Entryset.iterator  ()  while (It.hasnext  ()) {Map me = (map) It.next  ()  System.out  .println  (Me.getkey  () + "::::"  +me .getvalue  ()) }  

Tips for using collections:
See array is the structure of arrays, there is a corner mark, query fast. The
See link is the linked list structure: The deletion speed is fast, and has the unique method. AddFirst; addlast; Removefirst (); Removelast (); GetFirst (); GetLast ();
See hash is a hash table, you want to hash value, we should think of uniqueness, It is important to think that the elements deposited into the structure must overwrite the Hashcode,equals method.
See tree is a binary tree, it is necessary to think of a sort, you want to use the comparison.
The two ways to compare:
One is comparable: Overwrite CompareTo method;
One is comparator: Overrides compare method.
Linkedhashset,linkedhashmap: These two sets ensure that the hash table is stored in the order and in the order in which it is fetched. When does the
collection work?
When an element is stored, use collection. The map collection is used when there is a mapping between the stored objects. The
is guaranteed to be unique, using set. Not guaranteed to be unique, use list.

Collections: The presence of it provides more functionality to the collection operation. This class does not need to create objects, and internally provides static methods.
static method:
Collections.sort (list);//list collection for the natural ordering of elements.
Collections.sort (list,new Comparatorbylen ());//Sort by the specified comparer method.
Class Comparatorbylen implements comparator{
public int compare (String s1,string s2) {
Int temp = S1.length ()-S 2.length ();
return Temp==0?s1.compareto (S2): temp;
}
}
Collections.max (list);//returns the element with the largest dictionary order in the list.
int index = collections.binarysearch (list, "zz");//binary lookup, returns the corner label.
Collections.reverseorder ();//reverse-reverse sort.
Collections.shuffle (list);//random displacement of the position of the elements in the list.
to convert a non-synchronized collection to a synchronous collection: XXX synchronizedxxx (XXX) in collections;
List synchronizedlist (list);
Map Synchronizedmap (map);
Principle: Define a class that adds all the methods of the collection to the same lock and returns.
Collection and Collections:
Collections is a class under Java.util, a tool class for a collection class that provides a series of static methods for finding, sorting, replacing, Thread-safe operations such as converting a non-synchronized collection to synchronous. The
Collection is a Java.util interface, which is the parent interface of various collection structures, and the interfaces that inherit from it are mainly set and list, providing some operations on the collection, such as inserting, deleting, judging whether an element is its member, traversing, and so on.

Arrays:
The tool class used to manipulate array objects is a static method.
Aslist method: Converts an array into a list collection.
String[] arr = {"abc", "KK", "QQ"};
List List = Arrays.aslist (arr);//convert arr array to list collection.
What are the benefits of converting an array into a collection? Using the Aslist method, the array is transformed into a set;
The elements in the array can be manipulated through the methods in the list collection: IsEmpty (), contains, indexOf, set;
Note (limitations): Arrays are fixed length, can not be used to add or delete objects, etc., can change the length of the array function method. such as add, remove, clear. (The report does not support operation exception unsupportedoperationexception);
If the reference data type is stored in an array, the elements directly as a collection can be manipulated directly with the collection method.
If the base data type is stored in the array, Aslist will present the array entity as a collection element.
Set of variable groups: using the method in the collection interface: ToArray ();
If the data length of the specified type passed to ToArray is less than the size of the collection, then the ToArray method automatically creates a data of that type, and the length is the size of the collection.
If the length of the array that is passed the specified type is greater than the size of the collection, then the ToArray method does not create a new array, directly uses the array, and stores the elements in the collection in the array, and the other is the default value NULL for the location of the stored element.
Therefore, the best way to pass an array of the specified type is to specify an array of equal length and size.
What are the benefits of turning a collection into an array? Limit the additions and deletions to the elements in the collection, as long as you get the elements.

Dark Horse programmer--javaapi

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.