1. Summary of this week's study
2. Written assignment 1. ArrayList Code Analysis 1.1 explains ArrayList's contains source code
Public Boolean contains (Object o) { return0; }
- According to the source know contains function is a kind of judge whether there is a certain content function, it uses the function ondexof, through the judgment, existence this content returns True, otherwise returns false.
1.2 Explanation e Remove (int index) source code
Public E Remove (int index) { Rangecheck (index); Modcount++ ; = Elementdata (index); int nummoved = size-index-1; if (nummoved > 0) System.arraycopy (elementdata, index+1, Elementdata, index, nummoved); elementdata[null// clearto let GC do it work return OldValue; }
- The Remove function according to the source code is computed by calculating the position of the element in the array, then using the System.arraycopy () function to get the adjusted array, then resetting the element in the primitive group to be deleted to null.
1.3 In conjunction with 1.1 and 1.2, do you need to consider the specific types of elements when storing data in ArrayList?
- ArrayList can store data without regard to the basic data type, because the type ArrayList can store is object, and the parent class of all classes, so if we keep ArrayList constraints, we can not consider the type of data stored.
1.4 Analysis Add source code, answer when the internal array capacity is not enough, what to do?
Public Boolean Add (e e) { + 1); // increments modcount!! elementdata[size++] = e; return true ; }
- According to the source code, we know that when we refer to the Add function, it first uses the Ensurecapacityinternal function to determine if expansion is required, if you need to expand the reference ensureexplicitcapacity function.
1.5 Analyzing the private void Rangecheck (int index) source code, why should the method be declared private and not declared as public?
Private void rangecheck (int index) { if (index >= size) Throw New indexoutofboundsexception (outofboundsmsg (index)); }
- This function is intended to determine that the array is out of bounds and is only required to be used inside the class, and it does not need to be externally available.
2. HashSet principle 2.1 Adding an element to the HashSet (hash set), how is its storage location determined? which methods need to be called?
- When an element is added to a hash table, the position of the function is stored and hashed by Hashcode first, but the position has a value that determines whether the two are equal, the equality does not need to be joined, and the unequal is used to determine the next position using the method of resolving the Harlem conflict.
- The calling method is the method of calculating hashcode and the method of resolving the hash conflict.
2.2 What is the time complexity of adding elements to HashSet? is O (n)?
- Elements that join HashSet are not determined by traversal, but rather by hashcode to calculate location, so its time complexity is not O (n).
3. ArrayListIntegerStack3.1 compare himself write Arraylistintegerstack with oneself in the topic set jmu-java-04-Object-oriented 2-advanced-polymorphic, What is the difference between an interface and a custom interface arrayintegerstack in an internal class?
- Before writing the custom interface implementation, most of the methods are written by themselves and refer to a fixed array to store the data, and this time I write this program when the real dynamic array ArrayList, its encapsulation is strong many methods can be called and the length of the array is variable.
3.2 Combined with this problem simply describe the benefits of the interface, 3.1 as an example of detailed explanation, not generalities.
- Interface as a collection of abstract methods, enabling us to implement the abstract functions defined by the interface under different circumstances, as far as the Arraylistintegerstack interface is concerned, we use different forms to implement these abstract functions in terms of previous and present problems, thus embodying polymorphism, which is the interface adapted to different environments.
4. Stack and Queue4.1 write a function to determine whether a given string is a palindrome, be sure to use the stack (take advantage of the existing classes in the Java collection), but you cannot use the Java stack Class (for specific reasons) and arrays. Please paste your code, the class name is main your school number.
4.2 Set of jmu-java-05-collection of bank business queue simple Simulation (paste only the key code). Be sure to use the queue interface and indicate which implementation class you are using for the queue interface?
- The subject uses the LinkedList implementation class to implement the queue interface.
5. Count the number of words in the text and sort them alphabetically by word output the number of words in the 5-2 statistic text in the Jmu-java-05-collection and sort the output by the alphabetical order of the words (do not appear in the job for large sections of code)
5.1 Experimental Summary
- This experiment uses TreeSet to store the data and to sort the words alphabetically and then output them.
3. Code cloud and PTA3.1. Codes Cloud Code submission record
3.2 PTA Problem set complete situation diagram
3.3 Count the amount of code completed this week
| Week Time |
Total code Amount |
New Code Volume |
total number of files |
number of new files |
| 1 |
0 |
0 |
0 |
0 |
| 2 |
0 |
0 |
0 |
0 |
| 3 |
0 |
0 |
0 |
0 |
| 4 |
437 |
437 |
7 |
7 |
| 5 |
905 |
468 |
13 |
6 |
| 6 |
1294 |
389 |
21st |
8 |
| 7 |
1678 |
384 |
30 |
9 |
| 8 |
2184 |
40W |
42 |
12 |
201621123021 Java programming Eighth week of study summary