当前位置:网站首页>List set & UML diagram
List set & UML diagram
2022-07-02 15:14:00 【Xiao afai_】
Catalog
Use ArrayList in remove Attention points of the method
UML The relationship and representation in the figure
3、 Aggregation and combination
5、 generalization (Generalization)
List Interface
Basic introduction
characteristic : The elements are in order , And repeatable
Traverse : Subscript ,foreach, iterator
Capacity expansion :
- Initial capacity 10, Load factor 0.5, Expansion increment 0.5 times
- New capacity = Original capacity + Original capacity * 0.5 , Such as ArrayList The capacity of is 10, Then the capacity after one expansion is 15
List Implementation class of
1、ArrayList
- Simple data structure , Over capacity Automatic expansion , The dynamic array
- ArrayList Diagram of expansion principle :
- ArrayList The capacity expansion mechanism is a balance between efficiency and space ,
- The internal implementation is based on the underlying array of objects
- No Suitable for random addition or deletion ( Displacement phenomenon )
- Apply to The maximum number of data is uncertain
- Random access quick 、 It's fast
- Thread unsafe ( When used as a member variable of a class )
2、LinkedList
- LinkedList Provide additional get,remove,insert Method stay LinkedList Head or tail of
- LinkedList Can be used as a stack (stack)【 It includes push,pop Method 】, queue (queue) Or two-way queue (deque)
- With Double linked list Realization , The list has no capacity limit , The allowed element is null, Thread unsafe
Two way linked list atlas :
fit Make random additions or deletions , Because the linked list structure will not shift when adding or deleting
3、Vector
Thread safety , But the parallel performance is slow ( Because it's locked ), Not recommended
4、CopyOnWriteArrayList
- When writing copy
- Thread safety
- ( Version of ) Final consistency , The real-time version cannot be consistent
- Than Vector High performance
- Suitable for Read more , Write less Scene
- Realized List Interface , Usage and ArrayList similar
- Copy a new array when writing , Finish inserting 、 Assign the new array to the original array after modification or removal
CopyOnWriteArrayList The illustration
Before looking at the picture, first understand the concept of reading and writing
- read : Only data is read , Do not modify data (CopyOnWriteArrayList The collection from the Central Plains is used to read )
- Write : Operate on the data to make the data change (CopyOnWriteArrayList in The new set is used to write )
stay CopyOnWriteArrayList For the new set all “ Write ” Once the operation of , The pointer to the original set will immediately point to the new set , Reflects the final consistency
Use ArrayList in remove Attention points of the method
remove characteristic :
- Remove subscript when passing in integer type
- Delete the corresponding element when the object type is passed in ( Such as Integer.parseInt(null))
Data preparation : For the convenience of demonstration , Need to have two or more identical elements next to each other
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);Use ArrayList in remove Several different ways of writing methods :
- Examples of wrong writing
for(int i=0;i<list.size();i++){
if(list.get(i)==3) list.remove(i);//list.get(i) Get the subscript i The elements of
}The reason for the error :ArrayList When adding and deleting Displacement phenomenon , When two identical elements 3 Adjacent time , first 3 After judgment and deletion , the second 3 The subscript of all the elements following it will move forward by one bit , So the second one 3 It's the first one 3 The position of the subscript , But the pointer has already judged the first 3 Whether the element in the location is 3 了 , Therefore, the second... Will not be deleted 3
for(Integer i:list){
if(i==3) list.remove(i);
}wrong Error reason : because ArrayList There's a variable in (modCount= The number of elements of the original array ) It also internally encapsulates an internal class (Itr), This inner class implements the iterator , When using foreach Method traversal , It uses ArrayList Iterators of inner classes , The inner class defines a variable that changes the number of times (expectedModCount), This variable is assigned an external value modcount Value , When using inner classes (Itr) When adding or modifying , Throw an exception , Its purpose is to prevent ArrayList The length changes .
In short , It is not recommended to use foreach Add and delete collections , It is more suitable for traversing data
- Examples of correct writing
for(int i=0;i<list.size();i++){
if(list.get(i)==3) list.remove(i--);
}The right reason : Used i--, That is to say After deleting The pointer will move forward one bit , Then go back to the deleted subscript position to judge , And this avoids because ArrayList The judgment omission caused by the displacement phenomenon of .
Be careful : Not to be used --i, because --i Will be in remove Method in Before deleting perform
for(int i=list.size()-1;i>=0;i--){
if(list.get(i)==3){
list.remove(i);
}
}The right reason : Traversal from the last element in the set to the first element is used Reverse traversal Method , Even though ArrayList The occurrence of the displacement phenomenon of cannot affect the deletion
The last way to write :ArrayList The collection is being deleted 、 When adding, etc , The dynamic displacement characteristics should be considered , Iterators are recommended , It will be safer
Iterator<Integer> it=list.iterator();
while(it.hasNext()){
if(it.next()==3){
it.remove();
}
}Be careful : Of the above code it.remove Don't write as list.remove(i)
UML chart
What is? UML chart ?
Unified modeling language (UML) It's a kind of Modeling language
Most models are based on Chart In a way that shows
Modeling diagrams usually contain Several blocks or boxes , Connecting line And as models Additional information Text for , stay UML The rules relate and extend to each other .
Why use UML chart ?
(1)UML It unifies various methods for different types of systems 、 Different views of different development stages and different internal concepts spot , Thus, the unnecessary differences between various modeling languages are effectively eliminated . It is actually a general modeling language , It can be widely used by users of many object-oriented modeling methods .
(2)UML Modeling ability is better than others object-oriented The modeling method is stronger . It is not only suitable for the development of general systems , And for parallel 、 The modeling of distributed system is particularly suitable .
(3)UML It's a modeling language , Not a development process .
UML Illustration example

UML The relationship and representation in the figure
1、 relation (Association)
Relevance is a kind of The relationship you have , It makes One class knows the properties and methods of another class ; Such as : Teachers and students , The relationship between husband and wife can be two-way , It could be one-way . A two-way association can have two arrows or no arrows , A one-way association has an arrow
The code is : Member variables
Arrows and points : With ordinary arrows solid , Point to the possessed
Above picture , Teachers and students are connected in two ways , The teacher has many students , Students may also have more than one teacher . But the relationship between students and a course is unidirectional , A student may take more than one course , Course is an abstract thing. He doesn't have students
The following figure shows self association :
2、 rely on (Dependency)
Dependency is a kind of Relationship used , namely The implementation of one class requires the assistance of another class , So try not to use two-way interdependence , It's usually One way dependency
Code representation : local variable 、 Method parameters or calls to static methods
For example, the method of one class uses the method of another class , Or local variables are used in the methods of a class
The stronger the relationship , The stronger the dependency
Arrows and points : Dotted line with arrow , Point to the user
3、 Aggregation and combination
It's all about The relationship between the part in the whole and the whole The closeness of , they Are a kind of relationship
Combine (Composition)
- It's the relationship between the whole and the part , but The part cannot exist alone without the whole . For example, the relationship between the company and the Department is the whole and part , There is no department without a company
- synthetic relation It is a kind of relation , yes A relationship stronger than an aggregation relationship , It requires that the object representing the whole in the common aggregation relationship is responsible for the life cycle of the object representing the part
- The code is : Member variables
- Arrows and points : A solid line with a solid diamond , Rhombus pointing to the whole
polymerization (Aggregation)
- It's the relationship between the whole and the part , And Parts can be separated from the whole and exist alone . For example, the relationship between the car and the tire is the relationship between the whole and the part , The tires can still be left in the car .
- Aggregation is a kind of association , It's a strong correlation ; Association and aggregation are grammatically indistinguishable , We must examine the concrete logical relations
- The code is : Member variables
- Arrows and points : A solid line with a hollow diamond , Rhombus pointing to the whole
4、 Realization (Realization)
It's a kind of The relationship between class and interface , Express A class is an implementation of all the features and behaviors of an interface
The arrow points to : Dotted line with triangular arrows , The arrow points to the interface
5、 generalization (Generalization)
It's a kind of Inheritance relationships , Express The relationship between general and special , It designates How subclasses specialize all the characteristics and behaviors of the parent class . for example : Tiger is a kind of animal , There are not only tiger's characteristics, but also animal's common characteristics
The arrow points to : A solid line with a triangular arrow , The arrow points to the parent class
Summary
The order of strength and weakness of various relationships :
generalization = Realization > Combine > polymerization > relation > rely on
This picture below UML chart , It vividly shows all kinds of class diagram relationships :

边栏推荐
- TiDB 软件和硬件环境建议配置
- Table responsive layout tips
- Record an interview
- 03_ Linear table_ Linked list
- 03_線性錶_鏈錶
- 871. Minimum refueling times: simple priority queue (heap) greedy question
- Practical debugging skills
- qml 弹窗框架,可定制
- Why can't programmers who can only program become excellent developers?
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
猜你喜欢
![[C language] explain the initial and advanced levels of the pointer and points for attention (1)](/img/61/1619bd2e959bae1b769963f66bab4e.png)
[C language] explain the initial and advanced levels of the pointer and points for attention (1)

Have you learned the wrong usage of foreach

21_ Redis_ Analysis of redis cache penetration and avalanche

【C语言】详解指针的初阶和进阶以及注意点(1)

Tidb data migration tool overview

MFC 定时器使用

【无标题】LeetCode 2321. 拼接数组的最大分数

btrace-(字节码)动态跟踪工具

04_ Stack

LeetCode 2320. Count the number of ways to place the house
随机推荐
C thread transfer parameters
How to test tidb with sysbench
Solve the problem that El radio group cannot be edited after echo
Introduction to C language -- array
C# richTextBox控制显示最大行数
14_Redis_乐观锁
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
MFC A对话框调用B对话框函数并传参
C#延时、在线程中开启定时器、获取系统时间
数据分析常见的英文缩写(一)
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Deploy tidb cluster with tiup
Table responsive layout tips
Introduction to mathjax (web display of mathematical formulas, vector)
为什么只会编程的程序员无法成为优秀的开发者?
AtCoder Beginner Contest 254
How does CTO help the business?
.NET Core 日志系统
学习使用php实现公历农历转换的方法代码









