7. Arrays

Source: Internet
Author: User
Tags array definition array length

  1. Knowledge Review
    1. Learning variables before, we know that variables can store different data as needed, but it can store only one data at a time
    2. If a program needs to process multiple data at the same time, if you use only variables, you need to define a lot of variables that are inefficient and the array will solve the problem.
  2. What is an array
    1. Data collection of the same data type
    2. Classification
      1. One-dimensional arrays
      2. Two-dimensional arrays (in fact, one-dimensional arrays)
  3. The role of arrays
    1. can store multiple data of the same data type
  4. How to use arrays
    1. One-dimensional array definition syntax (thinking same variable, syntax different)
      1. First declaration, then assign the value
        1)

      2. declaring and assigning values
        1)

    2. Basic features of one-dimensional arrays
      1. Line record data (Access data based on a single subscript)

      2. Array subscript
        1) It is the index of each array in the array, starting with 0, plus 1
      3. The length of the array
        1) array can store the number of data
        2) Its value is the value of the length property of the array
        3) length = maximum small Mark + 1
    3. One-dimensional array of additions and deletions (traversal), the operation of the data on the four kinds of
      1. Increase
        1) Once the array is defined, the length cannot be changed and the array cannot be added.
      2. By deleting
        1) Once the array is defined, the length cannot be changed, the data cannot be deleted, only the data can be modified to the initial value.
      3. Change
        1) Re-assignment: array name [subscript] = new value
      4. Check (various methods)
        1) Iterate through the array (check all data)
        1. Using the method provided by the JDK (take doctrine: almighty Baidu)
        1. The ToString (array name) method of the arrays class to output all the data in the array

         2. 自己编码(还是提倡吧,有益大脑,实际开发就拿来主义吧!)     1. 依次获取数组中的数据,对于这种重复性操作可以使用循环来完成     2. 语法         ![](https://images2018.cnblogs.com/blog/1009428/201808/1009428-20180826151407315-126747851.png)

        2) Find the extremum (check maximum and minimum value)
        1. Using the method provided by the JDK (take doctrine: almighty Baidu)
        Arrays.steam (array name). Max () or. min (). Getasint ()

              Arrays.sort(数组名);然后根据下标找极值,缺点是"改变了数组中元素顺序"         ![](https://images2018.cnblogs.com/blog/1009428/201808/1009428-20180826151431396-377784108.png) 2. 自己编码(先假定数组中下标为0的为极值,然后依次与其它数比较,找到真正极值后重新赋值给变量即可)     ![](https://images2018.cnblogs.com/blog/1009428/201808/1009428-20180826151436443-1269879976.png)

        3) sort (ascending)
        1. Using the method provided by the JDK (take doctrine: almighty Baidu)
        Arrays.sort (array name), need to import java.util.Arrays

         2. 自己编码      选择排序(选择数组中第一个数,然后依次与后面的数比较,把最小的放前面,最大放后面)         ![](https://images2018.cnblogs.com/blog/1009428/201808/1009428-20180826151512161-1987488509.png)      冒泡排序(相信元素比较,小的排前,大的排后)         ![](https://images2018.cnblogs.com/blog/1009428/201808/1009428-20180826151516791-392512180.png)         ![](https://images2018.cnblogs.com/blog/1009428/201808/1009428-20180826151523112-1052283852.png)

        4) Invert (descending)
        1. So simple, no API, write it Yourself
        1. Use a loop to remove from an array and then write to another array, paying attention to the transformation of the subscript (two ways)

         2. 如果,反转前是升序,那么反转后就是降序了

        5) Find the subscript of an element in the array (whether or not an element exists)
        1. Use the API (is a sorted subscript, but you can sort whether the element exists)

         2. 自己编码(使用循环依次遍历)     ![](https://images2018.cnblogs.com/blog/1009428/201808/1009428-20180826151543198-1424150428.png)

        6) Other operations
        1. Copy the specified range of the specified array to a new array. Contains the starting position but does not contain the end position
        1. Arrays.copyofrange (int[] arr,int from,int to); its return value is an array
        2.

    4. Common exceptions for one-dimensional arrays
      1. ArrayIndexOutOfBoundsException: Array index out of bounds exception, concise point (array subscript out of bounds)
        1) root cause: The array of operations does not have this subscript, but you do not want to access

      2. Nullpointexception: null pointer exception (empty object no longer points to heap memory)

    5. Two-dimensional array definition syntax (two-dimensional array nature is also a one-dimensional array, the underlying is processed by a one-dimensional array)
      1. First declaration, then assign the value

      2. declaring and assigning values
        1)
    6. The basic characteristics of two-dimensional arrays
      1. Matrix record Data (which is, of course, actually in memory or Linetype, which is easy to understand)
        1)
      2. Outer array subscript and lenght, subscript and length for each array in the inner layer (understanding the same dimension array)
        1) Subscript
        1.
        2) Array length
        1.
    7. Two-dimensional array additions and deletions (traversal)
      1. Increase
        1) Once the array is defined, the length cannot be changed and the array cannot be added.
      2. By deleting
        1) Once the array is defined, the length cannot be changed, the data cannot be deleted, only the data can be modified to the initial value.
      3. Change
        1) Re-assignment: array name [outer subscript] [inner subscript] = new value
      4. Check (all operations are converted into one-dimensional arrays later implemented: One more loop outside)
        1) Iterate through the array (check all data)
        1. Using the JDK: You can also use ToString () instead of using deeptostring ()
        1.
        2. Encode yourself
        1.
        2) Find extremum, sort, reverse, find
        1. The two-dimensional arrays are converted into one-dimensional arrays and then manipulated
    8. Summarize
      1. If you only know the length of the array, you can define the array in a declarative way, when the data stored in the array has a corresponding default value based on the type
      2. The operation of an array cannot be separated from a loop (the array itself is a repetitive store of data)
      3. The problem can be solved by means of dimensionality reduction, which can be considered in multidimensional way.
        1) The operation of the two-dimensional array is converted into a one-dimensional problem to solve
        2) The problem of one-dimensional array can be fixed, try to use multidimensional array to solve
      4. Java also has 3-dimensional, 4-dimensional arrays, but the actual use is not very useful (theory is endless)
        1)

7. Arrays

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.