javaSE --- array

1. Why have array

Now need statistics wages of employees of a company, such as calculating the average wage, to find the highest wages. Assuming that the company has 80 employees,
with the knowledge you learned earlier, the program first need to declare 80 variables to keep in mind are the wages of each employee, and then during the operation, this will seem very troublesome.
To solve this problem, Java provides for us to use an array
conclusion:
A plurality of storage array variables (elements) thing (container)
== This type of data more variables to be consistent ==

2. array definition

An array of a plurality of the same data type is a set of storage elements. It can also be seen as a container.
Arrays can either be stored basic data types, data types may be stored reference.

  • Array Definition Format
    Format 1: Data type [] array name;
    Format 2: Data type array name [];
    example:
    int[] a; 定义了一个int类型的数组a;
    int a[]; 定义了一个int类型的a数组;
    recommend the first embodiment is defined .

3. array initialization

  • 1. What is to initialize an array of
    arrays in Java must be initialized before use.
    The so-called initialization: that allocates memory for the array elements in the array, and assigned to each array element.
  • 2. Initialize the Category:
    • a: Dynamic Initialization: Only specified length, is given by a system initialization value
    • B: static initialization: the initialization value is given, the system determines the length
      Note: these two ways, only one of static and dynamic binding can not be performed
  • 3. Dynamic initialization format:
    数据类型[] 数组名 = new 数据类型[数组长度];
    数组长度其实就是数组中元素的个数。
    For example: int [] arr = new int [3]; defines an array of type int arr, this array can store three int type value.

4.Java difference in memory allocation and stack and heap

  • A: Stack: storing the local variables
    local variables: variables in the process of the method declaration or definition are local variables.
  • B: Heap: storage of all new things out
    characteristics:
    A: a new out every thing will assign a value to the system.
    b: Each variable has a default value of the
    byte, Short, int, Long - 0
    a float, Double - 0.0
    char - '\ u0000'
    Boolean - to false
    reference data types - null
    C: becomes garbage after use, wait its recycling garbage collector
  • C: The method of object-oriented area partially explain :()
  • D: Local district :( methods and systems related)
  • E: Register: (cpu use)

Illustrates a memory array of an array ☞ 5.)

Define an array, output array names and values ​​of the array elements, assigning to the array, the array element values ​​output again and the array name
Here Insert Picture Description

6 illustrates the memory array of two arrays ☞

Define two arrays, each array name and the output values ​​of the array elements, each assigned to two arrays, the output value of the element array and the array name again

Here Insert Picture Description

Illustrates a memory array of three ☞ 7. reference array 2

The first definition of an array Once defined, assigned to the array elements. After the assignment is completed, then the output array name and elements.
The second array is defined, Once defined, assigned to the array elements. After the assignment is completed, then the output array name and elements.
The third definition of the array, the address value of the first array is assigned to it. (Note that the same type), to re-assign the elements by name, the third array.
Finally, once again the output of the first array element and the array name.
Here Insert Picture Description

8. initialize an array of static initialization

  • A:静态初始化的格式:
    格式:数据类型[] 数组名 = new 数据类型[]{元素1,元素2,…};
    举例: int[] arr = new int[]{1,2,3};
    简化格式:
    数据类型[] 数组名 = {元素1,元素2,…};
    举例: int[] arr = {1,2,3};

9.数组遍历

public class ArrayDemo2 {
    public static void main(String[] args) {
        int[] arr = new int[]{10, 20, 30};
        for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
    }
}

10.二维数组概述和格式

10.1二维数组格式1

数据类型[][] 变量名 = new 数据类型[m][n];
m表示这个二维数组有多少个一维数组 必须写上
n表示每一个一维数组的元素个数 可选
举例:
int[][] arr = new int[3][2];
定义了一个二维数组arr
这个二维数组有3个一维数组,名称是arr[0],arr[1],arr[2]
每个一维数组有2个元素,可以通过arr[m][n]来获取
表示获取第m+1个一维数组的第n+1个元素
C:注意事项
A:以下格式也可以表示二维数组
a:数据类型 数组名[][] = new 数据类型[m][n];
b:数据类型[] 数组名[] = new 数据类型[m][n];
这两种格式不推荐使用
B:注意下面定义的区别
int x,y;
int[] x,y[];

区别是:
int[] x,y[];//定义了两个数组 一个是一维数组x 一个是二维数组y
x=new int[3];
y=new int[3][];

内存图解
Here Insert Picture Description

10.2二维数组格式2

二维数组格式2
数据类型[][] 变量名 = new 数据类型[m][];
m表示这个二维数组有多少个一维数组
这一次没有直接给出一维数组的元素个数,可以动态的给出。
举例:
int[][] arr = new int[3][];

Here Insert Picture Description

10.3二维数组格式

3 two-dimensional array format
数据类型[][] 变量名 = new 数据类型[][]{{元素…},{元素…},{元素…}...};
a simplified version:
数据类型[][] 变量名 = {{元素…},{元素…},{元素…}};
This is a static initialization format: specific element values specified by us, the length of the system to assign
Example:
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
int[][] arr = {{1,2,3},{5,6},{7}};

11. The two-dimensional array traversal

案例演示
	需求:二维数组遍历
	外循环控制的是二维数组的长度,其实就是一维数组的个数。
	内循环控制的是一维数组的长度。
	public class ArrayDemo2 {
    public static void main(String[] args) {
        int[][] arr = new int[][]{{10, 20, 30}, {5, 6}, {6, 8}};
        //二维数组的遍历
        for (int i = 0; i < arr.length; i++) {
            // System.out.println(arr[i]);
            for (int j = 0; j < arr[i].length; j++) {
                System.out.println(arr[i][j]);
            }
        }
    }
}

12. The two-dimensional array ☞ sum

  • A: Case presentation
    requirements: annual sales sum
    of a company in accordance with the quarter and month statistical data as follows: Unit (million)
    in the first quarter: 22,66,44
    in the second quarter: 77,33,88
    in the third quarter: 25,45,65
    fourth quarter: 11,66,99
public class ArrayDemo3 {
    public static void main(String[] args) {

       /* A:
        案例演示
        需求:公司年销售额求和
        某公司按照季度和月份统计的数据如下:单位(万元)
        第一季度:22, 66, 44
        第二季度:77, 33, 88
        第三季度:25, 45, 65
        第四季度:11, 66, 99*/

      // int[][] arr={{22, 66, 44},{77, 33, 88},{25, 45, 65},{11, 66, 99}};
        int[][] arr = new int[4][3];


        int[] one={22, 66, 44};
        int[] two={77, 33, 88};
        int[] three={25, 45, 65};
        int[] four={11, 66, 99};

        arr[0]=one;
        arr[1]=two;
        arr[2]=three;
        arr[3]=four;

        //遍历二维数组的元素,累加求和
        int sum=0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                sum+=arr[i][j];
            }
        }
        System.out.println("总销售额"+sum);

    }
}

  • B:
需求:打印杨辉三角形(行数可以键盘录入)
		1
        1 1
        1 2 1
        1 3 3 1
        1 4 6 4 1
        1 5 10 10 5 1
public class MyTest {
    public static void main(String[] args) {

        /*
        * 1.每一行的第一个元素和最后一个元素都是1
        * 2.从第三行开始,从第二列开始,中间的数等于上一行的前一列和本列之和
          3.每一行的数据可以看作一个一维数组
          然后把每一行的数据再用二维数组从起来。
        * */
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入行数");
        int n = scanner.nextInt();
        //构建二维数组
        int[][] arr = new int[n][n]; //
        //1.把第一元素和最后一个元素置为1
        for (int i = 0; i < arr.length; i++) {
            arr[i][0]=1;
            arr[i][i]=1;
        }

        //2.算出中间元素
        //从第三行开始,从第二列开始,中间的数等于上一行的前一列和本列之和
        for (int i = 2; i < arr.length; i++) {
            for (int j = 1; j <= i; j++) {
                arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
            }
        }

        //3,打印出三角形
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j <=i; j++) {
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

analysis: See law such an image
A: the first column and the last column is any line. 1
B: starting from the third row, each row of data is on its front and on it a row of the present column sum.

step:
A: First, define a two-dimensional array. If the number of lines is n, we first while the number of columns is defined as n.
The n data input from the keyboard.
B: for the first column and the last column assigned to any two-dimensional array row. 1
C: according to a rule of assignment of other elements
from the third row, it is on each data line before the present one on its column and row Sum.
D: traversing the two-dimensional array.

13. Questions

In Java parameter passing problem

public class MyTest2 {
    public static void main(String[] args) {
        //基本类型的数据,作为参数传递,形参的改变,不影响实参  值传递
        //引用类型作为参数传递,形参的改变,会影响实参  引用传递
        //定义两个局部变量
        int a = 10;
        int b = 20;
        //打印变量的值
        System.out.println("a: " + a + ",b: " + b);//1 10  20
        //调用方法 把 a 和 b 传递过去
        change(a, b);
        //继续打印变量的值
        System.out.println("a: " + a + ",b: " + b); //4.10 20
        //定义一个数组
        int[] arr = {1, 2, 3, 4, 5};
        //调用方法,把数组传过去
        change(arr);
        //输出数组中第二个元素的值
        System.out.println(arr[1]); //4
    }

    public static void change(int a, int b) {

        System.out.println("a: " + a + ",b: " + b); //2 10  20
        a = b;
        b = a + b;
        System.out.println("a: " + a + ",b: " + b); //3. 20 40
    }

    public static void change(int[] arr) {
        for (int x = 0; x < arr.length; x++) {
            if (arr[x] % 2 == 0) {
                arr[x] *= 2;
            }
        }
    }
}
Published 39 original articles · won praise 1 · views 518

Guess you like

Origin blog.csdn.net/love_to_share/article/details/104100669
Recommended