Array Overview
An array is a set of data that has the same data type. Each element in the array has the same data type. The length of the array is fixed. In Java, an array is also considered an object, although the base data type is not an object, but an array of basic data types is an object. The introduction of arrays in programming can be used to manage and manipulate data more efficiently. The array can be divided into one-dimensional arrays, two-dimensional arrays according to the dimensions of the array ...
Creation of Arrays
Int[] arr1={0,1,2}; int arr2[]={0,1,2}; int [] arr3=newint[3]; int [] arr4=newint[]{0,1,2}; P (arr1); P (ARR2); P (ARR3); P (ARR4);
Uninitialized array int type initial value is 0
common methods of arrays class
PackageCom.hanqi.maya.model;Importjava.util.Arrays; Public classARR { Public Static voidMain (string[] args) {intarr1[]={0,1,2,3,4,5}; Arrays.fill (arr1,0, 3, 1);//Replace the array with a value of 1 before starting from the No. 0 index to the 3rd indexp (arr1); intA=arrays.binarysearch (ARR1, 4);//find the number in the array by the dichotomy to return the index position, the array must be sorted, if not return the position should appear and minus signSystem.out.println (a); intarr2[]={9,1,2,3,4,5}; intarr3[]={9,1,2,3,4,5}; System.out.println (Arrays.equals (ARR2, ARR3));//determine if the array is the sameArrays.sort (ARR2);//sort an arrayp (ARR2); intArr4[]=arrays.copyofrange (arr2, 0, 3);//copy an array, starting at index 0 and before index 3p (ARR4); System.out.println (arrays.tostring (ARR2)); } Public Static voidPintarrs[]) { for(inti=0;i<arrs.length;i++) {System.out.print (Arrs[i]+","); } System.out.println (); }}
Array Sorting
PackageCom.hanqi.maya.model;Importjava.util.Arrays; Public classARR { Public Static voidMain (string[] args) {intarr1[]={0,1,2,3,4,5}; //Bubble Sort for(inti = 0; i < arr1.length; i++) { for(intj = 0; J < Arr1.length-1; J + +) { if(Arr1[j] < Arr1[j + 1]) { inttemp = arr1[j + 1]; Arr1[j+ 1] =Arr1[j]; ARR1[J]=temp; }}} p (arr1); //Direct Select Sort intarr2[]={9,1,2,3,4,5}; for(inti = 0; i < arr2.length; i++) { intIndexmax =i; for(intj = i + 1; J < Arr2.length; J + +) { if(Arr2[indexmax] <Arr2[j]) {Indexmax=J; } } inttemp =Arr2[i]; Arr2[i]=Arr2[indexmax]; Arr2[indexmax]=temp; } p (ARR2); //Reverse Sort intarr3[]={9,1,2,3,4,5}; intLen =arr3.length; for(inti = 0; i < LEN/2; i++) { inttemp =Arr3[i]; Arr3[i]= Arr3[len-i-1]; Arr3[len-I-1] =temp; } p (ARR3); } Public Static voidPintarr[]) { for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+","); } System.out.println (); }}
simple examples of arrays
//Student Class PackageCom.jihe; Public classStudent {PrivateString name; Private intAge ; PublicStudent () {} PublicStudent (String name,intAge ) { This. name=name; This. age=Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } }
Public classShuzu { Public Static voidMain (string[] args) {//TODO auto-generated Method StubStudent st[]=NewStudent[3]; Student S1=NewStudent ("Mr.a", 25); Student S2=NewStudent ("Mr.b", 28); Student S3=NewStudent ("Mr.C", 29); st[0]=S1; st[1]=S2; st[2]=S3; for(intx=0;x<st.length;x++) {System.out.println (St[x].getname ()+","+st[x].getage ()); } }}
Array Simple Example 2
Packagecom.hanqi.maya.test;ImportJava.util.Scanner; Public classTest { Public Static voidArr () {System.out.println ("Please enter five numbers, separated by", "the program will automatically output maximum, minimum, and sum, average." "); Scanner SC=NewScanner (system.in); intMax=0;; intMin=0; DoubleSum=0; DoublePj=0; String SA=Sc.nextline (); String[] Arr=sa.split (","); int[] arr1={0,0,0,0,0}; arr1[0]=integer.parseint (arr[0]); arr1[1]=integer.parseint (arr[1]); arr1[2]=integer.parseint (arr[2]); arr1[3]=integer.parseint (arr[3]); arr1[4]=integer.parseint (arr[4]); for(inti=0;i<arr1.length;i++) {sum+=Arr1[i]; if(Arr1[i] >max) {Max=Arr1[i]; } intJ=i+1; if(j<4) {min=arr1[0]; if(arr1[j]>=Arr1[i]) {min=Arr1[i]; }Else{min=Arr1[j]; }}} System.out.println ("Maximum value is:" +max); System.out.println ("Minimum value is:" +min); System.out.println ("Total is:" +sum); System.out.println ("The average number is:" +SUM/5); }}
Packagecom.hanqi.maya.test;ImportJava.util.Scanner; Public classXingzuo {Private StaticString[] Xzname = {"Aries", "Taurus", "Gemini", "Cancer", "Lion", "Virgin", "Libra", "Scorpio", "Shooter", "Capricorn", "Aquarius", "Pisces" }; Private StaticString[] Xzdate = {"March 21-April 20", "April 21-May 20", "May 21-June 21", "June 22-July 22", "July 23-August 22", "August 23-September 22" "," September 23-October 22 "," October 23-November 21 "," November 22-December 21 "," December 22-January 19 "," January 20-February 18 "," February 19-March 20 " }; Public Static voidCheck () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter the constellation you want to inquire about"); string[] Ch= (Sc.nextline ()). Split ("block");//try not to use Kanji as a segmentation intx=0;//Judging Variables for(intj=0;j<ch.length;j++){ for(inti = 0; i < xzname.length; i++) { if(Ch[j].equals (Xzname[i])) {System.out.println (Xzname[i]+ "The birth date of the seat is:" +Xzdate[i]); X++;//match succeeded once, variable +1Sc.close (); }/*else{System.out.println ("Please enter the correct 12 constellation name, only two kanji"); }*/ } } if(x!=ch.length) {//if the value of the final variable is not equal to the length of the array, it means that some of the input values are not matched toSYSTEM.OUT.PRINTLN ("You may not have entered all the constellations"); } }}
Package com.hanqi.maya.test; Public class Testmain { publicstaticvoid main (string[] args) { // TODO auto-generated method stub Test.arr (); Xingzuo.check (); }}
An array of Java