// [Format 1] Data type [] array name // Example int [] ARR Double [] ARR Long [] ARR Boolean [] ARR String [] ARR
Format II:
// [2] format of data type array name [] // Example
The basic format of the array initialization
Package com.itheima_01; / * array: a storage model for storing a plurality of the same type of data format definition :( recommended) data type [] array name; example: int [] arr; array initialization: A: The so-called initialization: that allocates memory for the array elements in the array, and each array element assigned to B: initializing the array dynamic initialization static initialization dynamic initialization: only specify the length of the array initialization, the system assign initial values to an array format: data types [ ] variable name = new data type [array size]; example: int [] ARR = new int [. 3]; * / public class ArrayDemo { public static void main (String [] args) { int [] ARR = new new int [ 3 ]; / * Left: int: Description element type of the array is an int []: Description This is an array arr: This is the name of the array on the right: new new: an array of application memory space int: Description element type of the array is an int [] : this is an array Description 3: the array length, in fact, the number of elements in the array * / // output array name System.out.println (ARR); @ [@ 880ec60 the I // element of the output array System. Out.println (ARR [0 ]); System.out.println (ARR [ . 1 ]); System.out.println (ARR [ 2 ]); } }
summary
-
What arrays are?
Array for storing a plurality of the same type of data container.
-
It features an array?
Only store the same type of data.
-
Definition Format Array
-
Formats: Data Type [] variable names recommended
-
Format II: data type variable name [] to understand
-
-
Features index value of the array?
Array index is starting from 0, +1 each time.
-
It can be used to initialize an array of dynamic definition array
Format: Data Type [] = new variable name Data type [length | capacity]
比如:double[] arr = new double[3];
left:
double: represents the data type of the array can be stored.
arr: just a variable name
right:
new application memory space. new is a keyword, all objects must create keywords you want to use.
double: represents the data type of the array can be stored.
[]: Represents the array
3: represents the capacity of the array of containers, you can store up to three data.
--------
What is the dynamic initialization?
== only the length of a given array, the default initial value given by the system. ==
Dynamic initialization format?
Data Type [] array name = new new data type [array length];
// array of dynamic initialization // define the length of the array 3 int int [] = ARR new new int [3 ]; // define the length of the double array 5 double [] = ARR new new double [5 ]; // defined length 5 char array char [] = ARR new new char [5 ]; // define the length of the array of strings 4 string [] = ARR new new string [4];
int[] arr = new int[5];
-
The left side of the equal sign:
-
int: data type array
-
[]: This is on behalf of an array
-
arr: name represents the array
-
The right of the equal sign:
-
new: an array of memory open space
-
int: data type array
-
[]: This is on behalf of an array
-
-
5: represents the length of the array
The role of each memory area
Zone Name | effect |
Stack memory (Stack) | Runtime memory usage of the method, the method of storing variables and parameters. |
Heap (Heap) | Storage object or array, new out of the data stored in the heap memory. |
Methods district | Information storage class file loaded. |
register | For CPU use, and we have developed unrelated. |
Native method stacks | JVM when using the operating system functions to use, and we have developed unrelated. |
Now we just need to focus stack memory and heap memory.
Stack memory: the method is finished, the release of memory, stack memory is stored in a local variable.
Heap memory: data after use, waiting for the garbage collector, heap memory is stored out of the new data, arrays, objects.
The default value of the array initialization
Integer type: 0
Floating-point types: 0.0
Character types: null character
Boolean: false
Reference types: null String [] arr = new String [5];
summary
-
-
Stack memory: a method If you need to run, jvm for the method will open up a separate space in the stack memory, stack memory storage are local variables.
-
Heap memory: all through the array or object created new keywords, jvm will open up a separate memory space in the heap memory.
-
Methods district
-
register
-
Native method stacks
-
-
Features stack memory and heap memory to store data?
-
Stack memory: a method If you need to run, jvm for the method will open up a separate space in the stack memory, stack memory storage are local variables.
-
Heap memory: all through the array or object created new keywords, jvm will open up a separate memory space in the heap memory.
-
Multiple variables point to the same array object, then modify the elements affect each other?
-
It will affect each other.
What is static initialization?
When you create an array, the elements of the value determined directly.
When to use dynamic initialization:
When you create an array, the array elements can not be determined when the value of using dynamic initialization
When to use static initialization
When you create an array, have been determined value of the element, use static initialization
Static initialization format:
Data Type [] array name = new new data type [] {element 1, element 2, element 3 ...} // Example int [] ARR = new new int [] {10,20, 30,40};
Static initialization short form:
Data Type [] array name = {element 1, element 2, element 3 ...}; // Example int [] = {10,20, 30,40} ARR;
Basic use static initialization
/* 静态初始化: 初始化时指定每个数组元素的初始值,由系统决定数组长度 格式:数据类型[] 变量名 = new 数据类型[]{数据1, 数据2, 数据3, ……}; 范例: int[] arr = new int[]{1, 2, 3}; 简化格式:数据类型[] 变量名 = {数据1, 数据2, 数据3, ……}; 范例: int[] arr = {1, 2, 3}; */ public class ArrayDemo { 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]); } }
小结
-
静态初始化的特点?
数组一旦创建就已经指定了元素的值。
-
静态初始化的格式?
格式一:数据类型[] 变量名 = new 数据类型[]{元素1,元素2...}
格式二(推荐) : 数据类型[] 变量名 = {元素1,元素2....}
-
什么时候使用静态初始化与动态初始化?
动态初始化特点:创建一个数组对象,但是不指定数组的元素。
静态初始化的特点: 一旦创建数组对象,那么就已经指定了数组的元素。
答案: 根据实际需求而决定。 比如: 当前我需要从数据库中读取15条数据放入到数组中。
【练习】下面创建数组错误的是(C ) A、 int[] arr = new int[5]; B、 int arr[] = new int[5]; C、 int[] arr = new int[3]{1,2,3}; //int[] arr = new int[]{1,2,3}; D、 int[] arr = {1,2,3}; 【练习】下面为数组初始化正确的写法是( A ) A: double c[]=new double[]{1,2} B: double c[]=new double(1,2) //double c[]=new double[]{1,2} C: double c[]=new double[](1,2) //double c[]=new double[]{1,2} D: double c[]=new double{1,2} //double c[]=new double[]{1,2} E: double c[]=new double[2]{1,2} //double c[]=new double[]{1,2}
/* 索引越界:访问了数组中不存在的索引对应的元素,造成索引越界问题 ArrayIndexOutOfBoundsException 空指针异常:访问的数组已经不再指向堆内存的数据,造成空指针异常 NullPointerException null:空值,引用数据类型的默认值,表示不指向任何有效对象 */ public class ArrayDemo { public static void main(String[] args) { int[] arr = new int[3]; //System.out.println(arr[3]); //System.out.println(arr[2]); //把null赋值给数组 arr = null; System.out.println(arr[0]); } }
-
-
索引越界
-
空指针异常
-
-
是何种原因导致出现的?
-
索引越界: 访问了不存在的索引值, 索引值的范围:0~容量-1
-
空指针异常: 一个变量没有记录任何对象的内存地址,然后访问了对象的属性或者方法。
-
/* 遍历 获取数组中的每一个元素输出在控制台 获取数组元素 数组名.length */ public class ArrayTest01 { public static void main(String[] args) { //定义数组 int[] arr = {11, 22, 33, 44, 55}; //使用通用的遍历格式 for(int x=0; x<arr.length; x++) { System.out.println(arr[x]); } } }
/* 需求:求数组最大值 分析:打擂台思想 步骤: 1、定义临时变量max,存储数组最大值,并假设第一个元素为最大值。 2、遍历数组,从第二个元素开始,逐个与max比较。 3、如果该元素比max变量还大,将该元素赋值给max变量。 4、循环结束后,max中存储的就是数组的最大值。 */ package com.itheima_05; /* 获取最值 获取数组中的最大值 最小值同学们自己完成 */ public class ArrayTest02 { public static void main(String[] args) { //定义数组 int[] arr = {12, 45, 98, 73, 60}; //定义一个变量,用于保存最大值 //取数组中第一个数据作为变量的初始值 int max = arr[0]; //与数组中剩余的数据逐个比对,每次比对将最大值保存到变量中 for(int x=1; x<arr.length; x++) { if(arr[x] > max) { max = arr[x]; } } //循环结束后打印变量的值 System.out.println("max:" + max); } }
小结
-
-
使用静态初始化的方式创建一个数组
-
定义一个变量用于保存最大值,默认值是获取数组的第一个元素。
-
从索引值1开始遍历数组的元素,然后拿这个每个元素与最大值的变量进行比较,如果出现某个元素大于最大值,那么最大值的变量就改变为当前的元素。
-
输出最大值
-