Java BASIC Syntax---java array 0. Overview
Arrays: A reference data type that consists of the same type of data in order.
Data type: base data type + reference data type;
Reference data type: Class + interface + array;
One-dimensional arrays: declaration, creation, initialization, element reference, length
1. Array declarations
Syntax format for variable declaration: Variable type variable name;
Syntax format for array declarations: data type [] array name; (data type array name [])
int[] myIntArray; //首选方法(int myIntArray[];) //来源于C/C++,效果相同,但不是首选方法String[] strArray;
2. Array creation
The syntax format is first declared and then created
data type [] array name;
Array name = new data type [array length];
Syntax format two declarations of simultaneous creation of arrays
数据类型 [] 数组名 = new 数据类型 [ 数组长度 ];dataType[] arrayRefVar = new dataType[arraySize];// 1.使用 dataType[arraySize] 创建了一个数组;// 2.把新创建的数组的引用赋值给了变量 arrayRefVar。
int[] arr = new int[10]; //创建一个长度为10的整型数组arr//注意:数组的长度必须给定
3. Initialization of arrays
An array is declared at the same time as an array, which is called the initialization of the array;
int[] arr = {1,2,3,4,5,6,7,8,9,10};// 数组的长度就是初始化数组时所给数组元素的个数。
4. References to array elements
Access to array elements is accessed through the index.
Syntax format: array name [subscript];
Note: The subscript starts at 0 (the array index starts at 0, so the index value is from 0 to array.length-1);
5. Length of the array
int[] arr = {0,1,2,3,4,5,6,7,8,9};//属性 length 表示数组的长度,例如:arr.length;
6. Case 6.1 Case 1: Application of one-dimensional array
The array element default value is 0 when an int array is created;
The array element defaults to 0.0 when the double array is created;
When a char array is created, the array element defaults to;
array element defaults to NULL when String array is created;
public class Arraytest {public static void main (string[] args) {//declares an integer array int[] intarray; Declares an array of string types string strarray[]; Create array intarray = new int[5]; Strarray = new STRING[10]; Create an array while declaring an array double[] Doublearray = new Double[4]; Initialize array char[] Charray = {' A ', ' B ', ' C ', ' d '}; char[] Ch1array = new CHAR[5]; System.out.println ("The length of the Ch1array array is:" + ch1array.length); SYSTEM.OUT.PRINTLN (the 2nd element in the Ch1array array is: "+ ch1array[3]); System.out.println ("The length of the Charray array is:" + charray.length); SYSTEM.OUT.PRINTLN (the 2nd element in the Charray array is: "+ charray[1]); System.out.println ("The length of the Intarray array is:" + intarray.length); System.out.println ("The second element in the Intarray array is:" + intarray[1]); System.out.println ("The length of the Strarray array is:" + strarray.length); System.out.println (the 5th element in the Strarray array is: "+ strarray[4]); System.out.println ("The length of the Doublearray array is:" + doublearray.length); System. OUT.PRINTLN ("The 4th element in the Doublearray array is:" + doublearray[3]); System.out.println (); loop for integer array assignment for (int i = 0;i < 5;i++) {Intarray[i] = i + 2; }//Loop output integer array of element System.out.println ("integer array intarray element values are:"); Array subscript out of bounds (array subscript out of range) Exception in thread ' main ' java.lang.arrayindexoutofboundsexception:5//at Com.ryanjie.ArrayTe St.main (arraytest.java:51)//for (int i= 0;i < 6;i++)//{//System.out.print (Intarray[i] + " ");//}//System.out.println (); for (int i = 0;i < 5;i++) {System.out.print (Intarray[i] + ""); } System.out.println (); The length of the}output:ch1array array is: 5 the 2nd element in the Ch1array array is: The length of the Charray array is: 4 Charray the 2nd element in the array is: B intarray The length of the array is : 5 Intarray The second element in the array is: 0 The length of the Strarray array is: The 5th element in the Strarray array is: the length of the null Doublearray array is: 4 Doublearray the first The 4 elements are: 0.0 the element values in the integer array Intarray are: 2 3 4 5 6
6.2 Case 2: Calculating the sum of the elements of an array
public class Arrayssum {public static void main (string[] args) {//integer array's summation and//define integral array int[] Intarray = new Int[5]; Start receiving data from the keyboard, assigning values to the element Scanner sc = new Scanner (system.in); for (int i = 0;i < intarray.length;i++) {System.out.println ("Please enter the value of" + (i + 1) + "element:"); Intarray[i] = Sc.nextint (); }//output intarray elements in the array System.out.println ("Elements in the array are:"); for (int i = 0;i < Intarray.length; i++) {System.out.print (Intarray[i] + ""); } System.out.println (); The sum of the elements in the output intarray array and int sums = 0; for (int i= 0; i < intarray.length;i++) {sum + = Intarray[i]; } System.out.println ("element of the Intarray array and is:" + sum); }}output: Enter the value of the 1th element: 100 Enter the value of the 2nd element: 100 Enter the value of the 3rd element: 50 Enter the value of the 4th element: 50 Enter the value of the 5th element: 150 The elements in the array are: The elements of the Intarray array and are:
6.3 Case 3: Finding the maximum value of an array element
public class Maxelement {public static void main (string[] args) {//Find the maximum value in an array//define an integer array//int[] int Array = {34,23,78,56,31,66}; int[] Intarray = new Int[6]; Reads array element values from the keyboard Scanner sc = new Scanner (system.in); for (int i = 0;i < intarray.length;i++) {System.out.println ("Please enter the value of the" + (i+1) + "element:"); Intarray[i] = Sc.nextint (); }//output intarray elements in the array System.out.println ("Elements in the Intarray array are:"); for (int i = 0;i < intarray.length;i++) {System.out.print (Intarray[i] + ""); } System.out.println (); int max = intarray[0]; for (int i = 1;i < intarray.length;i++) {if (Max < intarray[i]) {max = I Ntarray[i]; }} System.out.println ("The maximum value of the Intarray array element is:" + max); }}output: Enter the value of the 1th element: 34 Enter the value of the 2nd element: 23 Enter the value of the 3rd element: 78 Enter the value of the 4th element: 56 Please enter the 5th elementValue: 31 Enter the value of the 6th element: The elements in the Intarray array are: The maximum value of the Intarray array element is: 78
7. Foreach Loop
A foreach loop, also known as an enhanced loop, can traverse an array without using subscripts.
int[] arr = {1,2,3,4,5,6};for(int n:arr) System.out.print(n + " ");//每次把数组中的元素存放到n中,输出。循环遍历一遍就终止;
public class Test { public static void main(String[] args) { //定义整型数组 //int[] intArray = {34,23,78,56,31,66}; int[] intArray = new int[6]; //从键盘读取数组元素值 Scanner sc = new Scanner(System.in); for(int i = 0;i < intArray.length;i++ ) { System.out.println("请输入第 " + (i+1) + "个元素的值:"); intArray[i] = sc.nextInt(); } //foreach循环 for(int n:intArray) { System.out.print(n + " "); } System.out.println(); }}
8. Arrays as arguments to functions
The array is passed as a parameter to the method.
Call the Printfdoublearray method to print the Doublearray array:
public static void printfDoubleArray(double[] doubleArray) { for (double element: doubleArray ) { System.out.println(doubleArray[i] + " "); }}//调用printfDoubleArray方法打印doubleArray数组printfDoubleArray(new double[] {3.14,6.66,8,88,3.59,7.84});
9. Array as the return value of the function
An array can be used as the return value of a function.
Assigns the value of the element in the list array to the result array:
public atstic int[] reverse(int[] list){ //声明 result 数组 并创建 result 数组 int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result;}
10. Bubble Sort Bubblesort
public class Bubblesort {public static void main (string[] args) {//bubble sort Bubblesort int[] Intarray = {34, 23,78,56,31,66}; SYSTEM.OUT.PRINTLN ("array elements before sorting are:"); for (int element:intarray) {System.out.print (element + ""); } System.out.println (); bubblesort int temp; for (int i = 0, i < intarray.length; i++) {for (int j = 0; J < Intarray.length-i-1; J + +) {if (Intarray[j] > intarray[j+1]) {temp = Intarray[j]; INTARRAY[J] = intarray[j+1]; INTARRAY[J+1] = temp; }}}//output sorted array System.out.println ("sorted array elements are:"); for (int element:intarray) {System.out.print (element + ""); } System.out.println (); }}output: The array elements before sorting are: 34 23 78 56 31 66 The sorted array elements are: 23 31 34 56 66 78
Java BASIC Syntax---java array