Student number 2017-2018-20172309 "Program Design and Data Structure" Week 6 Learning Summary

Student number 2017-2018-20172309 "Program Design and Data Structure" Week 6 Learning Summary

Textbook learning content summary

  • Learned how to construct a simple array:
  • Definition: An array is a collection of data, each of which is called an element. Note: Arrays in Java are also objects, and the elements in them can be of any type, but the same array can only hold data of the same type.
  • step:
    • Declare a reference variable of type array.
      //int [] list;
    • Use the new statement to instantiate arrays, allocate space for them, and set default values ​​for each element. Usually 0. //list=new int[长度]
    • Initialize, with appropriate values ​​for each data. //list[0]=1; list[1]=2;
  • Actually, it can be solved in one sentence.int []list=new int[长度];
  • Learn about multidimensional arrays:
    • Comprehension: Suppose a hotel has three floors, and each floor has three rooms.
      int[][] room=new int[3][3];It's just that the index of the array starts at 0
    • Third floor: |First room|Second room|Third room|
    • Second floor: |First room|Second room|Third room|
    • First floor: |First room|Second room|Third room|
    • Essence: Multidimensional arrays can be understood as arrays of arrays. E.g:

      { {1,2,3},
        {4,5,6},
        {7,8,9} } 
  • Get a rough idea of ​​the bounds of an array:

    • The index of an array starts from 0. Suppose you set the length to 5 and you want to access the last element, you can access it with index value 4. But if you visit with 5, then...

      错误: ArrayIndexOutOfBountsException。

    • 数组名.lengthThe length of the array can be obtained through the array . length is an attribute of the array, which can only be read and cannot be modified!
  • copy of the array:

    • In the example 8.8 in the book, the copy of the array is briefly mentioned. The problem in the book is that the length of the array used is not enough. It is solved by doubling the length of the array and then copying the data.

      DVD[] temp=new DVD[collection.length*2];//collection是长度不够的那个数组,乘2让他的长度翻倍。
      for (int dvd=0;dvd<collection.length;dvd++)
       
       temp[dvd]=collection[dvd];

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: What the hell is an array of objects? How does it relate to two-dimensional arrays?
  • Solution to Problem 1: Array is a wonderful thing, and the data in it is even more wonderful. This element can be just an element or an object. Note: the array itself is also an object. This object can also contain other data. For example: construct a student class whose properties include name and age.

     public class Student
    {
    
     private String username;
     private int age;
    
      public Student(String username, int age)
     {
      this.username = username;
      this.age = age;
      }
      后面还有getUsername()、getAge()、toString()方法没写

    Now let's define two student objects and build an array of Students.
    student s1=student("Xiao Ming", 21);
    student s2=student("Da Ming", 22);

     String[] student=new String[2];

    Assign value to the array:
    student[0]=s1;
    student[1]=s2;
    It is represented by UML diagram:

    it is very similar to the two-dimensional array, which is equivalent to the array in the above two-dimensional array example.

  • Question 2: How to understand this code:

     for(Book myBook : library)
      System.out.println(myBook);      
  • Problem 2 Solution: This thing was mentioned in Chapter 6, in the for-each loop in 6.4.1. In the above code, he is dealing with issues involving iterators. This code can be understood like this: For each book in the library, output its related information. The variable mybook takes the value of each Book object in the collection in turn, and the loop body can handle it accordingly. It is also equivalent to:

        Book myBook;
        while (bookList.hasNext())
        {
         myBook=bookList.next();    
        System.out.println(myBook);
        }
  • Question 3: How to understand the variable length parameter list?
  • Problem 3 solution:

    Variable parameters: It is suitable for situations where the number of parameters is uncertain and the type is determined. Java treats variable parameters as arrays.

    Note: Variable parameters must be at the back. When there are more than one parameter, they must be placed at the front such aspublic static int add(int x,int ...args)

    Features:

    1.只能出现在参数列表的最后;
    2...位于变量类型和变量名之间,前后有无空格都可以;
    3.调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法体中一数组的形式访问可变参数。

Problems and solutions in code debugging

  • Question 1:
  • Problem 1 Solution: XXXXXX
  • Question 2: XXXXXX
  • Problem 2 Solution: XXXXXX
  • ...

code hosting


Summary of last week's exam mistakes

  • Error 1:
正确答案:B          我的答案:C 
 错误原因:在C选项中,如果x=0的话,它又将给x赋值为零。而题目说的是把它放在一边。
  • Error 2:

     正确答案:A   我的答案:D
     理解情况:break语句只会跳出所在的循环,并执行下一个循环。

- Wrong question 3:

    正确答案:B    我的答案:A
     理解情况:我们也可以用比较运算符比较char类型数据,String类型数据只能用compareTo()、equals()、equalsIgnoreCase()进行比较。

- Wrong question 4:

      正确答案:E
       尽管写这样的switch语句是不寻常的,但它是完全合法的。switch语句执行的常规规则适用于在交换表达式表达式后执行匹配的case子句。在此之后,所有后续的条款都是按顺序执行的,因为没有中断语句来终止开关/案例的执行。

- Wrong question 5:

    正确答案:A   
     在Java中,就像在大多数语言中一样,循环语句基本上都是等价的(几乎可以互换)。它们的主要差异(s)与何时评估控制条件以及是否有用于递增/更新的语法有关。
  • Mistake 6:

           正确答案:A
           我的理解:switch语句是从第一条Case开始匹配,直至停止的语句。
  • Error 7:

           正确答案:B
            正确理解:break用法不是开发必须的。甚至许多开发商认其为不良用法。

Others (perception, thinking, etc., optional)

This chapter looks simple, but it is actually quite difficult. Just like doing a question, you can see the answer at a glance, but it is impossible to do it in practice. In the process of doing after-class examples, I found that the format and usage of many previous codes could not be written, so we should hurry up and review the previous content.

Reviewed classmates blog and code

  • Pair study this week
    • 20172310
    • pairing photos
    • Pair learning content
      • XXXX
      • XXXX
      • ...

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated) important growth
Target 5000 lines 30 articles 400 hours
the first week 075/200 1/1 05/20
the second week 560/500 1/2 13/38
The third week 972/1000 1/4 21/60
the fourth week 694/1666 1/5 21/90
fifth week 1544/3095 1/6 30/90
Week 6 600/3627 1/7 30/120

references

  1. Introduction and Reference of Arrays in Java
  2. Introduction to variable-length parameter lists
  3. Java mean and standard deviation calculation
  4. Brief use of Markdown

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324420538&siteId=291194637