Java Advanced-Settings (1)

Source: Internet
Author: User
Tags getmessage shallow copy

>arrays

Basic array
1. The common array is generated from the main () function, and the index of the array subscript cannot exceed the range of 0 to int
2. ArrayIndexOutOfBoundsException exception occurs when a program attempts to access the first or last data of an array.
(equivalent to Linked list)
3. Unfortunately, the array does not have Java source code. It is an ordered collection of object objects.
Declaration/Initialization
1. The type of the array has a base type. The object type. The declaration for an array simply needs to be added []
2. Can be int[] test, int []test, int test[] three forms.
3. The definition of an array must be declared in the number. Arrays are usually defined using loops or assignments.
Array of primitive type/object type
An array of primitive types maintains a sequential list of data, while object data exists in the object, and the array simply refers to the address of the object.
Multidimensional arrays
1. Like an array reference object, a multidimensional array is required if you want to maintain multiple contiguous arrays.


2. The initialization of multidimensional arrays also specifies the length. For example, a 2-dimensional array, the 1th subscript indicates how many arrays are maintained, and the 2nd subscript represents maintenance
The number inside the array.


3. The value of a multidimensional array must be determined by multiple dimensions together.



variable initialization: when the type array is specified, memory is assigned to the memory array
and give the default variable, generally by type specified, such as int specified as 0,double to 0.0d,string is null;

To pass an array to the method and get the return value:
When a parameter is passed, the system passes the array reference to the method, so you can change its value in the array.

Copying and cloning arrays:
Since the array is already fixed in initialization, you must use replication or cloning when you want to scale the arrays.
The system provides a tool method for copy, which provides the target of the source stream. Start position and copy length;
Throw arrayindexoutofboundsexception when data length is not enough to replicate
System.arraycopy (original,0, resultarray,0,original.length);
The system provides the same copy method Array.clone () to achieve replication.

In an array of types, a copy is a copy of a value, whereas in an object array, only a shallow copy of the object is made;

/** * @author Lean @date: 2014-10-16 */public class Copyandclonearrays {public static void main (string[] args) throws Cl onenotsupportedexception {Copyingarray ();} private static void Copyingarray () throws clonenotsupportedexception {int[] array1={1,2,3,4,5};int[] array2={ 1,2,3,4,5,6,7,8,9};//system.out.println (array1.length+ "" +doublearray (array1). length);//system.out.println ( array2//.length+ "" +doublearray (array2). length); Book[] array3={new Book (new Price), new book (New Price (30))}; Book[] Array4=doublearray (ARRAY3); array3[0].price.value=18; System.out.println (Array3[0].price);  System.out.println (Array4[0].price);} Static int[] Doublearray (int[] original) {int[] resultarray=new int[original.length*2]; System.arraycopy (original,0, resultarray,0,original.length); return resultarray;} Static book[] Doublebookarray (book[] original) {book[] resultarray=new book[original.length*2]; System.arraycopy (original,0, resultarray,0,original.length); return resultarray;} Static book[] Doublearray (book[] Original) {return (book[]) original.clone ();}} Class Price implements Cloneable{public int Value;public price (int value) {this.value = value;} @Overrideprotected Object Clone () throws Clonenotsupportedexception {price Obj=null;try {obj= () Super.clone ();} catch (Exception e) {System.out.println (E.getmessage ());} return obj;} @Overridepublic String toString () {return value+ "";}} Class Book implements Cloneable{public Book (price) {this.price = Price;} Public, price, @Overrideprotected Object Clone () throws Clonenotsupportedexception {book Obj=null;try {obj= (book) Super.clone ();} catch (Exception e) {System.out.println (E.getmessage ());} Obj.price= (Price) Price.clone (); return obj;}}



Array references and inference for equality:
> When a variable refers to an array, for example a refers to Arrray1, then assign a to the variable b,b the same reference array1.
> When two objects refer to an array at the same time, infer that they can use a==b; when two objects pass
Clone copy to get an array, then they refer to different memory address, can only use Equals ();

Arrays and strings:
In C, the char[] array is always used to represent a string, whereas in Java, arrays and strings are mutually capable
String1.tobytearray () Converts a string to an array.

New String (byte[]) to create a string;


Array Reflection:

/** * @author Lean  @date: 2014-10-16   */public class Arrayreflection {public static void main (string[] args) {//che Ckarray ();//define array//reflectnewinstance ();//Access Value//trygetset ();} private static void Trygetset () {int[] data={1,2,3,4}; Array.setint (data,2, 12); Array.setint (data,4, 12); System.out.println (Array.get (data, 2));} private static void Reflectnewinstance () {float[][] bowling= (float[][]) array.newinstance (float[].class,4); for (int i = 0; i < bowling.length; i++) {bowling[i]= (float[]) array.newinstance (Float.type, i+1);} System.out.println (bowling[2].length);} private static void Checkarray () {final int[] a={1,2,3}; Class Objclass=a.getclass (), if (Objclass.isarray ()) {class Componentclass=objclass.getcomponenttype (); System.out.println (Componentclass.getname ()); System.out.println (Array.getlength (a));}}}



>the Vector Classes
----Vector----
1. Create vectors to specify initial capacity and growth, and also to have a list of packages, such as:
Vector v=new Victor (arrays.aslist (array));
2.Vector follows the standard of the queue
Be able to join add (obj)/addelement () to the end of the queue,
Ability to add index location by index Add (index,obj)/insertelement (Obj,index)
It is also possible to add a collection queue to the end of AddAll (list);
3. Since the vector cannot be added to the original type, in the new version of the JDK, the system will be actively boxed for ourselves; print
Vector using ToString ();
4. Removing vectors can
Removes the Remove (obj)/removeelementat (index) by reference;
Removes the Remove (index)/removeelementat (index) by index;
By deleting part RemoveAll (list)/removerange (from,to);
All Delete Clear ()/removeallelement ();
Keep Collection selected to delete other Retainall (list)
5. Replace set (Index,obj)/setelement (Obj,index)
6. View/Set Queue size ()/setsize ()
7. Storage capacity: capacity is used to cope with the frequent increase or decrease of dynamic queue; If the queue capacity is too small, you should add
The queue is preceded by a ensurecapacity (int mincapacity) expansion, if the capacity is not general
Adds 1 time times the capacity; unlike size (), size represents the current number. Capacity represents how much can be put. When
completes the queue operation. You can call TrimToSize () to optimize the reduction of space.
Invariance of 8.Vector:
If there is a variable for reading only, the collections provides Unmodifiablelist (v) to prevent change;
9. Enumeration of elements
Public enumeration elements ().
while (E.hasmoreelements ()) {
Process (E.nextelement ());
}
Extended Abstractlist
Iterator i = V.iterator ();
while (I.hasnext ()) {
Process (E.next ());

}


10. Iterative values for multi-dimensional vectors:
MyType o = (MyType) ((Vector) Vector.elementat (3)). ElementAt (2);

11. Test vector elements:
Inference exists: Contains (obj)/containsall (list);
Index: indexOf (obj) assumes that there are 2 indexes that get the 1th of the order
Reverse index: lastIndexOf (obj) as on reverse
12.Copy and Clone
Call Clone () direct copy or Copyinto (array) to convert the arrays before indirectly copying

13. Intercept the vector.
Public List sublist (int fromIndex, int toindex)

14. Use Equals () to infer whether the vector content is equal;


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Java Advanced-Settings (1)

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.