20,182,310 eighth week learning summary

20,182,310 2019-2020-1 "Object-oriented programming and data structures," the eighth week of learning summary

Learning content summary

1. Generic
directed through before polymorphism through inheritance, and Object is the parent class of all types, so long as an array of type Object point, then the array can theoretically save all types of data, which is a in extreme cases
, however, when the use of Object, prone to problems between brothers conversion type, the compiler does not complain but will run-time error.
Generics: a class can be defined, and can save data operation, but only if the type is determined when instantiated (typically represented by a generic T)

class Box<T>
{
    T类型对象的声明与操作代码
}

Box defined when data to be used, to achieve it by way of example of a particular type, instead of T

Box box1=new Box ;
Box box2=new Box ;
...
2. Find the linear method
linear search method is to start from scratch to find, compared with each of the elements of the list until you find the target element or look to the end have not found.
The following method achieves a linear search. This method returns a Boolean value, if true, the element is found, otherwise false, meaning not found.

public static <T>   
 boolean linearSearch(T[] data, int min, int max, T target)
    {
        int index = min;
        boolean found = false;

        while (!found && index <= max) 
        {
            found = data[index].equals(target);
            index++;
        }

        return found;
    }

Binary search
binary search is to find the middle of the sorted list starts (lookup cell is sorted) instead of starting from one end or the other end of. By comparison, identify viable alternatives and again reduce by half the amount of the element, in the same way continue searching until you finally found the target element or viable candidates no longer exists.
The key is to find the binary comparison of each half will be deleted viable candidates.
The following method implements a binary search, wherein the minimum index and the maximum index of the array defines a portion for lookup (possible candidates) in the.


public static <T extends Comparable<T>>  
        boolean binarySearch(T[] data, int min, int max, T target)
    {  
        boolean found = false;
        int midpoint = (min + max) / 2;  // determine the midpoint

        if (data[midpoint].compareTo(target) == 0)
            found = true;

        else if (data[midpoint].compareTo(target) > 0)
        {
            if (min <= midpoint - 1)
                found = binarySearch(data, min, midpoint - 1, target);
        }
        
        else if (midpoint + 1 <= max)
            found = binarySearch(data, midpoint + 1, max, target);

        return found;
    }
}

Note: In this algorithm, midpoint = (min + max) / 2, when min + max values are obtained when the base, will be automatically converted to an int, ignore the fractional part, i.e. to determine when the midpoint of the selected index two first intermediate values (smaller one).
Compare to find the algorithm
for linear search and binary search, the best situation is exactly the target element is the first element of our investigation, the worst case are also not in the target group. Thus, the linear search time complexity is O (n), a binary search time complexity is O (log2 (n)).
When n is large, that is, when a particularly large number of elements, binary search will greatly improve efficiency. When n relatively small, linear look good debugging easier and does not require sorting, and thus also on small issues commonly used linear search.
3. Sort
Sort: based on certain criteria, the set of items arranged in a certain predetermined order.
Sequential ordering: generally using a pair of nested loops to sort n elements, takes about n ^ 2 comparisons.
Logrank: sorting the n elements typically takes about nlog2 (n) comparisons.
Three common sort order:
① Selection Sort, ② insertion sort, ③ bubble sort;
two common logarithmic Sort:
① quick sort, ② merge sort
choose Sort: find a minimum (or maximum) element by repeatedly , and place it into the final position to be sorted elements.
Insertion sort: by the sub-lists before an element repeatedly inserted into the already sorted, the realization of sorting element.
Bubble sort: by repeatedly comparing adjacent elements and swap them to sort to the element.
Quick Sort: unsorted by elements divided into two partitions, each partition and then recursively to sort.
The average time complexity of quick sort is O (nlogn). All the average time complexity of O (nlogn) algorithm, quicksort average performance is the best.

4. In the JavaAPI classes ArrayList and LinkedList class is implemented by a list of different underlying structure.
Class ArrayList and LinkedList classes implement the java.util.List interface. Some methods List interface as shown:

Textbook learning and problem-solving process

  • Problems 1: Queue Interface and add two methods offer any different in exception handling.
  • Problem 1 Solution add operations to ensure that the queue has given element. If a given element is not added to the queue, the operation will throw an exception.
    offer the operator given element inserted into the queue, if successful insertion, returns true, otherwise returns false.

  • Question 2: ASL meaning
  • Problem 2 Solution: the average length (Average Search Length, ASL): Specifies the key needs and expectations of conduct comparing the number of keywords, called search algorithm to find the average length of time to find success. Used to measure the efficiency of the algorithm.

Code debugging and problem solving in the process

  • Question 1: About null
  • Problem 1 Solution: null value is not any instance of an object, so the following example returns false, no matter what type of variable declarations.
    S = null String;
    IF (the instanceof String S) after forced uploaded,: wq save, and then can be re-git push.
  • Question 2: use lists to achieve an ordered list of add operation when a null pointer exception.
  • Problem 2 Solution:
public void add(T element) {
    if (!(element instanceof Comparable))
        throw new NonComparableElementException("OrderedList");

    Comparable<T> comparableElement = (Comparable<T>)element;

    LinearNode<T> current = head;
    LinearNode<T> previous = null;
    LinearNode<T> node = new LinearNode<>(element);


    while (( current != null) && (comparableElement.compareTo(current.getElement()) > 0))
    {
        previous = current;
        current = current.getNext();
    }

    if (previous == null )
    {
        head = node;
    }
    else {
        previous.setNext(node);
    }

    node.setNext(current);

    count++;
    modCount++;
}

Insert ordered list of codes, after running debugging, error position here.

while ( comparableElement.compareTo(current.getElement()) > 0 && current != null )

This is the error code beginning. Found that, during the time while judgment is to be compareTo comparing the size of the operation, rather than to determine whether current is empty. So, when current is empty, there have been direct null pointer exception.
So just need two conditions are mutually reversed click.

Code hosting

Last week exam wrong question summary

Last week, no exams, so there is no wrong questions.

Pair peer review and

Grading

  • Worth learning problems or blog:
    • Learning: In conclusion, there are problems with detailed pictures, and I always forget the screenshot, problems appear in the code can also find their own sources of error in time.
    • Question: learning content summary is not detailed enough.
  • Worth learning or problems in your code: None
  • Based on score, I give this blog scoring: 15 points. Scores are as follows:
  1. Proper use Markdown syntax (1 point):
  2. Elements range (1 point) template
  3. Textbook learning and problem solving process, a problem plus 1 point ()

  4. The code debugging and problem solving process, a problem plus 1 point ()
  5. Week over 300 branches valid code (plus 2 points)
  6. Other plus points:
    • Feelings, experience does not leave large empty 1 point
    • Typesetting fine plus one point
    • Progress bar records the learning time and improve the situation of 1 point
    • There are hands-on writing new code, add 1 point
    • After class choice has verified 1 point
    • Learning the wrong questions in depth, add 1 point
    • Comments seriously, I can point out problems and blog code plus 1 point
    • Pair learning authentic plus 1 point

Comments had students blog and code

Other (perception, thinking, etc., optional)

Knowledge is the most recent study of the data structure, corresponding to the amount of code becomes more up job and classroom practice has changed much. But the blog is supposed to earnestly fulfill, and I found myself a bit messy code repository cloud need to tidy up. keep working hard.

Learning progress bar

The number of lines of code (add / accumulate) Blog amount (add / accumulate) Learning time (add / accumulate) Important growth
aims 10,000 lines 30 400 hours
the first week 155/200 2/2 20/20 Initial grasp of linux command, java applets and debugging jdb
The second three weeks 470/625 2/4 20/40 Society is defined using the scanner defined class
the fourth week 1444/2069 2/4 20/60 IDEA and to download and install plug-ins, learn TDD debugging, writing test code
fifth week 1917/3986 2/8 20/80 Learn to write a simple client and server
Sixth Week 1324/5310 1/9 20/100 Java encapsulation, inheritance, polymorphism
Week Seven 2795/8105 3/12 40/140 Stacks, linked lists
Eighth Week 1135/9240 1/13 40/180 Select sort other sort, search, etc.

Guess you like

Origin www.cnblogs.com/tursws/p/11791642.html