201521123105 Seven Week Java learning summary

Source: Internet
Author: User

1. Summary of this week's study

2. Written work

1.ArrayList Code Analysis

1.1 Explaining ArrayList's contains source code

The following is the contains source code for ArrayList:

Public boolean contains (Object o) {
return indexOf (o) >= 0;
}
public int indexOf (Object o) {
if (o = = null) {
for (int i = 0; i < size; i++)
if (elementdata[i]==null) return i;
} else {
for (int i = 0; i < size; i++)
if (O.equals (Elementdata[i]))
return i;
}
Return-1;
}
Public boolean equals (Object obj) {
return (this = = obj);
}
A: Contains is a method used to determine whether a ArrayList contains an object or an element, where indexof is used to find the object or where the element is located, and if the object or element exists, the IndexOf method returns its position, if it does not exist, The -1,contains method is then returned based on the return value of the IndexOf method to determine whether the object or element is contained, and if the return value of the IndexOf method is >=0, the Contains method returns True if the return value of the IndexOf method < 0, the Contains method returns false.
1.2 interprets the source code for e Remove (int index)

Public E Remove (int index) {
Rangecheck (index);
modcount++;
E OldValue = (e) elementdata[index];
int nummoved = size-index-1;
if (nummoved > 0)
System.arraycopy (Elementdata, index+1, Elementdata, index,nummoved);
Elementdata[--size] = null; Let GC do it work
return oldValue;
}
A: First find the element you want to delete based on the location you specify, then remove the element, move all subsequent elements to the left, and subtract the length by 1.
1.3 In conjunction with 1.1 and 1.2, what types of elements do you need to consider when storing data ArrayList?

A: ArrayList does not need to consider the type of element when storing data.
1.4 Analysis Add source code, answer when the internal array capacity is not enough, what to do?

The following is the source code for add:

public void Add (int index, E element) {
if (Index > Size | | | Index < 0)
throw New Indexoutofboundsexception (
"Index:" +index+ ", Size:" +size);
Ensurecapacity (size+1); Increments modcount!!
System.arraycopy (Elementdata, index, Elementdata, index + 1,
Size-index);
Elementdata[index] = element;
size++;
}
A: The Add method is to determine whether the position that needs to be added to the new element exceeds the capacity of the array and, if so, expands the array capacity to add 1.
1.5 Analyzing the private void Rangecheck (int index) source code, why should the method be declared private and not declared as public?

The following is the source code for private void Rangecheck (int index):

private void Rangecheck (int index) {
if (index >= size) {
throw new RuntimeException ("range out");
}
}
A: Because the Rangecheck with private decoration can not be arbitrarily modified by the outside world, and have privacy, not be arbitrarily called, if the public to decorate, then the method can be seen by the outside world, and can be arbitrarily called

2.HashSet principle
2.1 Add an element to the HashSet (hash set), how is its storage location determined? which methods need to be called?
A: When adding a new object to a HashSet (hash set), first calculate the hashcode code of the object to be incremented based on the hash function, and the value is used to get a position with the element to hold the current object. If there is nothing in that position, then hashset that the element does not exist in the collection and adds it directly. If there is already an element in that position, then the elements that are ready to be added to the collection are compared with the elements at that location by using the equals method. If the equals method returns false, then the element is not present in the HashSet, then hashed again, and the element is placed in the address computed after the hash. If the Equals method returns True, the element already exists in HashSet, and the element is no longer added to HashSet. This procedure requires that the equals method and the Insert method be called.

3.ArrayListIntegerStack
Title Setjmu-Java-05-集合The 5-1 Arraylistintegerstack
3.1 Compare yourself writing arraylistintegerstack with yourself in the title setjmu-Java-04-面向对象2-进阶-多态、接口与内部类Topics in 5-3自定义接口ArrayIntegerStack, what's the difference? (Do not appear large code) A: The difference is that the former uses the Arrayli ST's dynamic Array, does not need to determine whether the stack is full, directly using the ArrayList internal method, the latter is already defined the length of the array, purely is the operation of the logarithmic group.
3.2 Simple description of the benefits of the interface.                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                             &NBSP;           A: For the subject, two arrayintegerstack methods have the same name, but are different implementations of the Integerstack interface. So it can be seen that one of the advantages of the interface is to complete the function is similar to the specific implementation of different situations, you can use the same method name, different implementations, according to the requirements of different ways to achieve.

4.Stack and Queue
4.1 Write a function to determine whether a given string is a palindrome, be sure to use the stack, but not use the Java stack Class (for specific reasons to search). Please paste your code, the class name is main your school number.

Package Huiwen;

Import Java.util.Scanner;

Class Stackstring {
public int maxSize;
Private char[] array;
private int top;

PublicStackstring(int maxSize) {This.maxsize = maxSize;Array =NewChar[maxsize]; top =-1;}PublicvoidPush(Char i) {Array[++top] = i;}Publicchar pop () { return (array[top--]);} public int peak () {return (array[top]);} public boolean isEmpty () {return (top = = -1);} public boolean isFull () {return (top = = maxSize-1);}    

}

public class Main201521123086 {
public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);
System.out.println ("Please enter a string:");
String s = scanner.next ();
stackstring stack = new stackstring (S.length ());
Char[] C = S.tochararray ();
for (int i = 0; i < c.length; i++) {
Stack.push (C[i]);
}
System.out.println ();
Boolean B = true;
for (int i = 0; i < stack.maxsize; i++) {
if (c[i]! = Stack.pop ()) {
b = false;
Break
}
}
if (b)
System.out.println ("The string is a palindrome!") ");
Else
System.out.println ("The string is not a palindrome!") ");
}
}

5. Count the number of words in the text and sort them alphabetically by word output
5-2 the jmu-Java-05-集合 number of words in the statistics text and sorted alphabetically by the word output (do not appear large code)

while (input.hasNextLine()) {            String str = input.next();            if (str.equals("!!!!!")){                break;} else if (!str.contains(str)) str.add(str); }5.1 实验总结:这题关键就是使用了Set的自然排序实现类TreeSet,此类可以对添加的对象进行默认排序。
3. Code submission record on code Cloud and PTA Experiment Summary

Topic Set:jmu-Java-05-集合

3.1. Code Cloud codes Submission record

3.2. PTA Experiment
 

201521123105 Seven Week Java learning summary

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.