2nd Chapter Array

Source: Internet
Author: User
Tags array definition array length

1.1 Overview of arrays
Requirement: Now we need to count the salary of a company's employees, such as calculating average wages, finding the highest wage, etc. Assuming that the company has 80 employees, using the knowledge previously learned, the program first needs to declare 80 variables to remember each employee's salary, and then in the operation, this will be very troublesome. To solve this problem, Java provides an array for us to use.
So what exactly is an array? What are the characteristics? Through the above analysis: we can get the following two words:
An array is something (a container) that stores multiple variables (elements)
The data types of the multiple variables must be consistent
1.2 Array Definition Format 1.2.1 array concept
An array is a container that stores multiple elements of the same data type.
Arrays can store either the base data type or the reference data type.
1.2.2 The definition format of an array
Format 1: Data type [] array name;
Format 2: Data type array name [];
Note: These two definitions are done, and there are no element values in the array.
1.3 Initialization of the array 1.3.1 Array initialization overview:
Arrays in Java must be initialized before they can be used.
Initialize: Allocates the memory space for array elements in arrays and assigns values to each array element.
Initialization of the 1.3.2 array 1.3.2.1 dynamic initialization: Only the array length is specified when initializing, and the initial value is assigned by the system arrays
Format: data type [] Array name = new data type [array length];
The length of the array is actually the number of elements in the array.
Example:
int[] arr = new INT[3];
Int[] arr = {A-i};
Explanation: An array of type int is defined, which can hold a value of 3 int type.
1.3.2.2 Case Code Three:

package com.itheima_01;/* * 数组:存储同一种数据类型的多个元素的容器。 * * 定义格式: * A:数据类型[] 数组名; * B:数据类型 数组名[]; * 举例: * A:int[] a; 定义一个int类型的数组,数组名是a * B:int a[]; 定义一个int类型的变量,变量名是a数组 * * 数组初始化: * A:所谓初始化,就是为数组开辟内存空间,并为数组中的每个元素赋予初始值 * B:我们有两种方式对数组进行初始化 * a:动态初始化        只指定长度,由系统给出初始化值 * b:静态初始化        给出初始化值,由系统决定长度 * * 动态初始化: * 数据类型[] 数组名 = new 数据类型[数组长度]; */public class ArrayDemo {public static void main(String[] args) {//数据类型[] 数组名 = new 数据类型[数组长度];int[] arr = new int[3];/* * 左边: * int:说明数组中的元素的数据类型是int类型 * []:说明这是一个数组 * arr:是数组的名称 * 右边: * new:为数组分配内存空间 * int:说明数组中的元素的数据类型是int类型 * []:说明这是一个数组 * 3:数组的长度,其实就是数组中的元素个数 */}}

1.3.2.3 Static Initialization: Specifies the initial value of each array element when initialized, and the system determines the length of the array 1.3.2.4 case code four:

package com.itheima_01;/* * 静态初始化的格式: * 数据类型[] 数组名 = new 数据类型[]{元素1,元素2,...}; * * 简化格式: * 数据类型[] 数组名 = {元素1,元素2,...}; * * 举例: * int[] arr = new int[]{1,2,3}; * * 简化后: * int[] arr = {1,2,3}; */public class ArrayDemo2 {public static void main(String[] args) {//定义数组int[] arr = {1,2,3};//输出数组名和元素System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);}}

1.4 Memory allocations for arrays 1.4.1 JVM memory Partitioning
Java programs need to allocate space in memory at run time. In order to improve the efficiency of computing, space is divided into different regions, because each region has a specific way of processing data and memory management.
Stack store local Variables
Heap storage new out of things
Method area (object-oriented advanced speaking)
Local method area (and system-dependent)
Register (for CPU use)

1.4. Memory graph of 21 arrays
Defines an array, outputting array names and elements. Then assign values to the elements in the array, and output the array names and elements again
1.4.2.1 Case Code Five:

package com.itheima_01;/* * 需求:定义一个数组,输出数组名及元素。然后给数组中的元素赋值,再次输出数组名及元素。 */public class ArrayTest {public static void main(String[] args) {//定义一个数组int[] arr = new int[3];//输出数组名及元素System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);//给数组中的元素赋值arr[0] = 100;arr[2] = 200;//再次输出数组名及元素System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);}}

1.4.2.2 Code Memory Plots:

1.4. Memory graph of 32 arrays
Defines two arrays, outputting array names and elements, respectively. Then assign values to the elements in the array, and then output the array names and elements separately.
1.4.3.1 Case Code VI:

package com.itheima_01;/* * 需求:定义两个数组,分别输出数组名及元素。然后分别给数组中的元素赋值,分别再次输出数组名及元素。 */public class ArrayTest2 {public static void main(String[] args) {//定义两个数组int[] arr = new int[2];int[] arr2 = new int[3];//分别输出数组名及元素System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr2);System.out.println(arr2[0]);System.out.println(arr2[1]);System.out.println(arr2[2]);//然后分别给数组中的元素赋值arr[1] = 100;arr2[0] = 200;arr2[2] = 300;//再次输出数组名及元素System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr2);System.out.println(arr2[0]);System.out.println(arr2[1]);System.out.println(arr2[2]);}}

1.4.3.2 Code Memory Plots:

1.4.42 Arrays of memory graphs pointing to the same address
Define two arrays, define an array, assign values, and output. The second array is then defined to assign the address of the first array to the second array. Then assign a value to the second array and output the names and elements of the two arrays again
1.4.4.1 Case Code VII:

/* * 需求:定义两个数组,先定义一个数组,赋值,输出。然后定义第二个数组的时候把第一个数组的地址赋值给第二个数组。 * 然后给第二个数组赋值,再次输出两个数组的名及元素。 */public class ArrayTest3 {public static void main(String[] args) {// 先定义一个数组,赋值,输出int[] arr = new int[3];arr[0] = 100;arr[1] = 200;arr[2] = 300;System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);// 然后定义第二个数组的时候把第一个数组的地址赋值给第二个数组int[] arr2 = arr;// 然后给第二个数组赋值arr2[0] = 111;arr2[1] = 222;arr2[2] = 333;// 再次输出两个数组的名及元素System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);System.out.println(arr2);System.out.println(arr2[0]);System.out.println(arr2[1]);System.out.println(arr2[2]);}}

1.4.4.2 Code Memory Plots:

 ![](http://i2.51cto.com/images/blog/201803/08/5a6fcea23754e314f614accf561fff76.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

1.5 array of elements using the 1.5.1 array access 1.5.1.1 case Code VIII:

package com.itheima_01;/* * 数组:存储同一种数据类型的多个元素的容器。 * * 定义格式: * A:数据类型[] 数组名; * B:数据类型 数组名[]; * 举例: * A:int[] a; 定义一个int类型的数组,数组名是a * B:int a[]; 定义一个int类型的变量,变量名是a数组 * * 数组初始化: * A:所谓初始化,就是为数组开辟内存空间,并为数组中的每个元素赋予初始值 * B:我们有两种方式对数组进行初始化 * a:动态初始化        只指定长度,由系统给出初始化值 * b:静态初始化        给出初始化值,由系统决定长度 * * 动态初始化: * 数据类型[] 数组名 = new 数据类型[数组长度]; */public class ArrayDemo {public static void main(String[] args) {//数据类型[] 数组名 = new 数据类型[数组长度];int[] arr = new int[3];/* * 左边: * int:说明数组中的元素的数据类型是int类型 * []:说明这是一个数组 * arr:是数组的名称 * 右边: * new:为数组分配内存空间 * int:说明数组中的元素的数据类型是int类型 * []:说明这是一个数组 * 3:数组的长度,其实就是数组中的元素个数 */`
System.out.println(arr); //[[email protected],地址值//我们获取到地址值没有意义,我要的是数组中的数据值,该怎么办呢?//不用担心,java已经帮你想好了//其实数组中的每个元素都是有编号的,编号是从0开始的,最大的编号就是数组的长度-1//用数组名和编号的配合我们就可以获取数组中的指定编号的元素//这个编号的专业叫法:索引//格式:数组名[编号] -- 数组名[索引]System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);}}

1.5.2 Array uses two small problems 1.5.2.1 case Code IX:

package com.itheima_02;/* * 两个常见小问题: * A:java.lang.ArrayIndexOutOfBoundsException * 数组越界异常 * 产生的原因:就是你访问了不存在的索引元素。 * B:java.lang.NullPointerException * 空指针异常 * 产生的原因:数组已经不指向堆内存的数据了,你还使用数组名去访问元素。 * 为什么我们要记住这样的小问题呢? * 编程不仅仅是把代码写出来,还得在出现问题的时候能够快速的解决问题。 */public class ArrayDemo {public static void main(String[] args) {// 定义数组int[] arr = { 1, 2, 3 };//System.out.println(arr[3]);//引用类型:类,接口,数组//常量:空常量 null,是可以赋值给引用类型的//arr = null;System.out.println(arr[1]);}}

1.6 One-dimensional array exercises 1.6.11-dimensional Array traversal 1.6.1.1 case code Ten:

package com.itheima_03;/* * 需求:数组遍历(依次输出数组中的每一个元素) * 获取数组中元素的个数:数组名.length */public class ArrayTest {public static void main(String[] args) {// 定义数组int[] arr = { 11, 22, 33, 44, 55 };// 原始做法System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);System.out.println(arr[3]);System.out.println(arr[4]);System.out.println("--------------------");// 用for循环改进for (int x = 0; x < 5; x++) {System.out.println(arr[x]);}System.out.println("--------------------");//为了解决我们去数数组中元素个数的问题,数组就提供了一个属性:length//用于获取数组的长度//格式:数组名.lengthSystem.out.println("数组共有:"+arr.length+"个");System.out.println("--------------------");for(int x=0; x<arr.length; x++) {System.out.println(arr[x]);}}}

1.6.2 array operation gets the most value:

1.6.2.1 Case Code 11:

package com.itheima_03;/* * 需求:数组获取最值(获取数组中的最大值最小值) */public class ArrayTest2 {public static void main(String[] args) {//定义数组int[] arr = {12,98,45,73,60};//定义参照物int max = arr[0];//遍历数组,获取除了0以外的所有元素,进行比较for(int x=1; x<arr.length; x++) {if(arr[x] > max) {max = arr[x];}}System.out.println("数组中的最大值是:"+max);}

}
1.7 Two-dimensional array 1.7.12-dimensional Array overview
Our Black Horse Programmer's Java Basic class has a lot of students in each class, so we can use arrays to store, and we have a lot of Java basic classes. This should also be stored in an array. How to represent such data? Java provides a two-dimensional array for our use.
This shows that the two-dimensional array is actually an array of one-dimensional arrays of elements.
1.7.22-D array format
Defining formats
data type [] array name;
Data type array name []; Not recommended
data type [] array name []; Not recommended
Initialization mode
data type [] Variable name = new data type [m][n];
data type [] Variable name = new data type [][]{{element ...},{element ...},{element ...}};
Simplified version format: data type [] Variable name = {{element ...},{element ...},{element ...}};
1.7.2.1 Case Code 12:

  Package com.itheima_04;/* * Two-dimensional array: An array of elements that are one-dimensional arrays. * * Definition Format: * A: Data type [] [] array name;        * B: Data type array name [];        Not recommended * C: Data type [] array name []; Not recommended * * How to initialize it? * A: Dynamic initialization * data type [] Array name = new data type [m][n]; * M indicates how many one-dimensional arrays of this two-dimensional array * n indicates how many elements of each one-dimensional array * B: Static initialization * data type [] Array name = new data type [][]{{element ...},{element ...},...}; * Simplified Format: * data type [] Array name = {{element ...},{element ...}},{element ...},...}; */public class Arrayarraydemo {public static void main (string[] args) {//data type [] Array name = {{element ...},{element ...},...}; Int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; System.out.println (arr); [[[[Email protected]system.out.println (Arr.length);////two-dimensional array of one-dimensional array number System.out.println (Arr[0]);//[Email  protected]system.out.println (arr[0].length); System.out.println (arr[1]);//[[Email protected]system.out.println (Arr[2]);//[email protected]// How do I get to an element of a two-dimensional array? System.out.println (Arr[0][0]); System.out.println (arr[1][1]); System.out.println (Arr[2][0]);}}  

1.7.32-dimensional array traversal
Traversal idea: First use a loop to iterate through each of the one-dimensional arrays stored in a two-dimensional array, and then iterate through the elements in that one-dimensional array using a loop for each of the traversed one-dimensional arrays
1.7.3.1 Case Code 13:

Package com.itheima_04;/* * Two-dimensional array traversal * * SYSTEM.OUT.PRINTLN (): Output content and newline * System.out.print (): Output * SYSTEM.OUT.PRINTLN (): NewLine *  /public class Arrayarraytest {public static void main (string[] args) {//define a two-dimensional array int[][] arr = {{1, 2, 3}, {4, 5, 6}, { 7, 8, 9}};//one-dimensional array name in two-dimensional array: two-dimensional array name [index]//arr[0] is actually the name of the first one-dimensional array in a two-dimensional array//arr[1] is actually the name of the second one-dimensional array in the two-dimensional array//arr[2] is actually the third of the two-dimensional The name of the dimension array//for (int x = 0; x < arr[0].length; × x + +) {//System.out.println (arr[0][x]);//}//System.out.println ("Hello"); /System.out.println ("World"),//System.out.print ("Hello");//System.out.print ("World");/*//the element for the first one-dimensional array for (int x = 0; x < arr[0].length; X + +) {System.out.print (Arr[0][x] + "");} System.out.println ();//The element for the second one-dimensional array for (int x = 0; x < arr[1].length; + +) {System.out.print (Arr[1][x] + "");} System.out.println ();//The element for the third one-dimensional array for (int x = 0; x < arr[2].length; + +) {System.out.print (Arr[2][x] + "");} System.out.println (); *///for (int. y=0; y<3; y++) {//for (int x = 0; x < arr[y].leNgth; X + +) {//System.out.print (Arr[y][x] + "");//}//System.out.println ();//}for (int y=0; y<arr.length; y++) {for (int x = 0; x < arr[y].length; + x + +) {System.out.print (Arr[y][x] + "");} System.out.println ();}}}

2nd Chapter Array

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.