Java BASICS (14) Set (1), java basics set

Source: Internet
Author: User

Java BASICS (14) Set (1), java basics set

Here are some of the knowledge points and code I summarized in my previous class. Most of the notes I think are very good and classic, sincerely hope that these will help those who want to learn!

It is inconvenient to upload code by module. There are also many things, and they are also clear! If you need it, you can leave your email in the comments. I will certainly send it to you for free! Thank you for making progress on this platform !! Remember that programmers are selfless !!!

Also very welcome to my blog to watch blog address: http://www.cnblogs.com/duscl/

/* 1: Object array (GRASP) (1) arrays can store both basic data types and reference types. The array used to store the reference type is called an object array. (2) Case: store 5 student objects in an array and traverse the array. 2: Collection (master) (1) Why is the Collection? We are learning Java-object-oriented-operating a lot of objects-storage-containers (arrays and StringBuffer)-arrays, And the array length is fixed, so it is not suitable for changing requirements, java provides a set for our use. (2) What is the difference between a set and an array? A: length difference array fixed set variable B: content difference array can be of the basic type or reference type set can only be of the reference type C: element Content Arrays can only store the same type of set. Different types can be stored (in fact, the set is generally stored in the same type) (3) inherited architecture of the set? Java provides different collection classes due to different requirements. The data structures of these multiple collection classes are different, but they all provide storage and traversal functions. We constantly extract their commonalities and finally form the inheritance architecture of the set. Collection | -- List | -- ArrayList | -- Vector | -- getting List | -- Set | -- HashSet | -- TreeSet (4) Overview of Collection functions (complete by yourself) A: Add function B: delete Function C: Judgment function D: Acquisition Function E: length function F: intersection (understanding) G: convert A set to an array (understanding) (5) traverse A of A Collection: convert A set to an array (understanding) B: iterator (set-Specific Method) (6) iterator A: it is the way to obtain elements of A set. B: It depends on the set. C: The principle and source code of the iterator. A: Why is it defined as an interface rather than an implementation class? B: Let's look at the internal class IMPLEMENTATION OF THE iterator. (7) Case Study of Collection set (Traversal method iterator) operation steps of the set: A: Create A Collection object B: Create an Element Object C: add an element to the set D: traversal set A: stores strings and traverses import java. util. collection; import java. util. arrayList; import java. util. iterator; public class CollectionDemo {public static void main (String [] args) {// create Collection object Collection c = new ArrayList (); // create and add element c. add ("hello"); c. add ("world"); c. add ("java"); // traverses the set Iterator it = c. iterator (); while (it. hasNext () {Strin G s = (String) it. next (); System. out. println (s) ;}} B: stores custom objects and traverses public class Student {private String name; private int age; public Student () {} public Student (String name, int age) {this. name = name; this. age = age;} // getXxx ()/setXxx ()} import java. util. collection; import java. util. arrayList; import java. util. iterator; public class StudentDemo {public static void main (String [] args) {// create a set Object Collection c = new ArrayList (); // create a Student object Student s1 = new Student ("Lin Qingxia", 27); Student s2 = new Student ("Feng Qingyang ", 30); Student s3 = new Student ("Liu Yi", 30); Student s4 = new Student ("Wu Xin", 25 ); student s5 = new Student ("Liu Xiaoqu", 16); // Add element c. add (s1); c. add (s2); c. add (s3); c. add (s4); c. add (s5); // traverse the set Iterator it = c. iterator (); while (it. hasNext () {Student s = (Student) it. next (); System. out. println (s. getNa Me () + "---" + s. getAge () ;}}3: Set (List) (master) (1) List is a Collection sub-interface feature: ordered (Consistent storage order and retrieval order ), repeatable. (2) Special Functions of List: (complete by yourself) A: Add function B: delete function C: Get function D: iterator function E: Modify function (3) the unique traversal function A of the List set is A combination of size () and get. B: Code demonstration // create a collection Object List = new ArrayList (); // create and add an element list. add ("hello"); list. add ("world"); list. add ("java"); // traverses the set Iterator it = list. iterator (); while (it. hasNext () {String s = (String) it. next (); System. out. println (s);} System. out. println ("----------"); for (int x = 0; x <list. size (); x ++) {String s = (String) list. get (x); System. out. println (s) ;}( 4) Special Functions of the List iterator; (understand) reverse traversal is supported, but forward traversal is required, so it is meaningless and basically not used. (5) concurrent modification Exception A: The symptom iterator traverses the set, and the Set Element B is modified in the set. The cause is that the iterator depends on the set, and the change iterator of the set is unknown. C: Solution a: iterator traversal, iterator modifier (ListIterator) element added in the previous iteration position B: Set traversal, set modification (size () and get ()) element added at the end of the Set (6) Common Data Structure A: stack first-in-one B: queue first-in-first-out C: Fast array query, slow addition/Deletion D: Slow linked list query, add/delete fast (7) subclass of List features (interview questions) ArrayList underlying data structure is an array, fast query, slow addition/deletion. The thread is not secure and the efficiency is high. The underlying data structure of the Vector is an array, which provides fast query and slow addition and deletion. Thread security and low efficiency. The underlying data structure of the shortlist is a linked list, which is slow in query and fast in addition and deletion. The thread is not secure and the efficiency is high. Who is it? Depends on requirements? Analysis: want security? Yes: Vector (this is not used even if it is required). Do not: ArrayList or multiple objects list queries. add or delete multiple ArrayList: ArrayList uses ArrayList if it does not know anything. (8) case of List set (Traversal method iterator and common for) A: store strings and traverse B: store custom objects and traverse *\

 

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.