1. Gets all the elements in the array, using the array traversal
The traversal of an array, usually with a for loop.
Public class Arraydemo { publicstaticvoid main (string[] args) { int newint[3]; for (int i=0;i<arr.length;i++) // the array name is the length of the array. The minimum value of the array's angular index is 0, and the maximum value is length-1. { System.out.println (arr[i]); }}}
Output:
0
0
0
2. Find the sum of the numbers in the array
Public class Arraydemo { publicstaticvoid main (string[] args) { int [] arr = {22,3,5,6,81,}; int sum=0; for (int i=0;i<arr.length;i++) { + =arr[i]; } SYSTEM.OUT.PRINTLN (sum);} }
Output: 117
3. Print out all the elements in the array and use "," to separate
Public classArraydemo { Public Static voidMain (string[] args) {int[] arr = {22,3,5,6,81,}; Printarr (arr); //calling Functions } Public Static voidPrintarr (int[] arr) {//a function that defines an output array. for(inti=0;i<arr.length;i++) { if(I>=0 & I<arr.length-1)//The output array element that is not the last element is judged, and a comma is added. Can be written i!=arr.length-1System.out.print (arr[i]+ ","); Else{//last element, output element only, no commaSystem.out.print (Arr[i]); } } }}
Output:
22,3,5,6,81
Operation of the Java array