Java Arrays
Arrays are used to store fixed-sizeSimilar typeThe Elements
Statement
Arrays must be declared before they can be used
Datatype[] arrayrefvar;//preferred [] put it in the end for a quick understanding of Java C + +
char[] ch; or char ch[];
char ch = new Char[arraysize];
1. Create a char array containing arraysize elements
2. Assign a reference to the new char array to the variable ch
Use of arrays
public class TestArr{ public static void main(String[] args){ double[] arr = {1.1,2.2,4.4,3.3}; for(double x:arr){ System.out.println(x); }//打印所有数组元素 double total = 0; for(int i = 0;i < arr.length;++i){ total = total + arr[i]; } System.out.println("Total = " + total); //计算并打印所有元素的和 double max = arr[0]; for(int i = 1;i < arr.length;++i ){ if (arr[i] > max) max = arr[i]; } System.out.println("Max = " + max); } //查找并打印最大元素值}输出结果:1.12.24.43.3Total = 11.0Max = 4.4
Array as the parameter of the function:
class A{ static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } }}public class TestA{ public static void main(String args[]){ int[] arr = {1, 2, 3, 4, 5, 6}; A.printArray(arr); }}输出结果:1 2 3 4 5 6
Organize the basics of Java--arrays 1