16. Array, class set framework

Source: Internet
Author: User
Tags array definition

1. Array definition methodsstatic declaration method int arr[]={1,1,2,3}; Dynamic declaration Method int Arr[]=new int[10];//creates an arr length of 10 2. Class Set Framework 2.1 DefinitionsA set of classes and interfaces for the JDK, located in the Java.utilPackages, which are used primarily for storing and managing objects. Compared to the array: capacity is automatically expanded and stored in many ways. 2.2 KindsHow to store Data: collections (set, unordered, no repeating elements), lists (objects in the list are sorted by index, can have duplicate objects), mappings (map, each)
Framework Theme Structure 2.3 Use Collection Interface method: 1) list-arraylist arraylist<string> Arraylist = new arraylist<string> ();              Only string can be stored;              <String> is a type parameter.   Method: Arraylist.add ("a");      Add a arraylist.get (1);       Remove the first object of the ArrayList arraylist.size (); Get ArrayList length
2)Set and HashSet (collection sub-interface, method with collection )Generates a String type HashSet object:hashset<string> HashSet = new hashset<string> (); A method of assembling unordered, traversing--iterator iterator generation: The iterator method that invokes the set object generates an iterator object that is used to traverse the entire set.             Collection is the iterator sub-interface. Build Iterator object:iterator<string> it = Set.iterator ();Builda surrogate object can take the elements of a set out of one, and the cursor moves backwards. methods in the Lterator
Hasnext (): The return value is Boolean, and there is no next element in the judgment iterator, which returns true. Next (): Remove the next element and the cursor moves down 1 bits;
map and HashMap:Object Establishment:hashmap<string,string> HASHMAP = new hashmap<string,string> (); Store key value pairsTransition up to map type:map<string,string> map = HashMap;               To map the key value pairs: Map.put ("1", "a");               Get the number of key-value pairs: int i = Map.size (); Get the value corresponding to the map key: String s = Map.get ("3"); 3.equals function 3.1 PositionIn the object class, object is also the parent class for all classes. So all classes inherit the Equals method. 3.2 EffectsThe function of "= =" is to determine whether two references point to the same piece of address in the heap memory, and equals to compare two items for equality.
Conform to the same content (in most cases) conditions: 1) The object type is the same (generated by the same class, can be compared using the instanceof operator), and 2) the member variables and values of two objects are the same.
3.3 replication equals function methodequals inherited in object is not suitable for all situations, in principle, replication.           public boolean equals (object obj) {if (this = = obj)//Determines whether to point to the same object in the heap memory, the same object content must be the same without judgment.           {return ture; } Boolean b = objinstanceof uesr;//Determines whether the preceding object is not the following type. The same classif (b)             {User u = (uesr) obj;if (this.age = = U.age && this.name.equals (u.name))//name to invoke equals,string replication OK, the judging value is not the same   return ture;             }Else             {return fulse;             }   Else          {return fulse;           }           }It is important to note that the comparison member variable, the base data type uses "= =" ratio, the reference data object (array, the string) usesequals 4.hashCode () methodis associated with equals. equals, the resulting hash code is the same as thethe object class. 5.toString () methodThe object is converted to a string, and the output string can be replicated. In the object class, the ToString method is called first when printing, and the returned string is printed.
We can print out the situation of our own objects.










From for notes (Wiz)

16. Array, class set framework

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.