1, the definition of the array
Data type array name []=new data type [size]
Data type array name []={element value, element value ...}
Method One:
float arry[]=new float[5];
Method Two:
int arr[]; or int[] arr;
Arr=new Int[5];
Method Three:
int a[]={1,1,2,3,5,8,13,21,34,55,89};
2. References to arrays (using)
Array name [subscript]
Public Static void Main (String args[]) { float arr[]={3,6,7.1f,1.1f,3}; float all=0; for (int i=0;i<arr.length;i++) { all + =arr[i]; } System.out.println ("average" + (all/arr.length)); }
The use of an array of objects, a comprehensive example is as follows:
Public classTest { Public Static voidMain (String args[])throwsException {Dog dogs[]=NewDog[4]; InputStreamReader ISR=NewInputStreamReader (system.in); BufferedReader BR=NewBufferedReader (ISR); for(inti = 0; i < dogs.length; i++) {Dogs[i]=NewDog (); System.out.println ("Please enter dog name:"); String name=Br.readline (); Dogs[i].setname (name); System.out.println ("Please enter the weight of the dog:"); String S_weight=Br.readline (); floatweight=float.parsefloat (s_weight); Dogs[i].setweight (weight); } floatAll_weight=0; for(inti=0;i<dogs.length;i++) {All_weight+=dogs[i].getweight (); } System.out.println ("Average Weight:" + (all_weight/dogs.length)); floatMax_weight=dogs[0].getweight (); intMaxindex=0; for(inti = 0; i < dogs.length; i++) { if(max_weight<dogs[i].getweight ()) {Max_weight=dogs[i].getweight (); Maxindex=i; }} System.out.println ("The most weight of the dog is:" +dogs[maxindex].getname () + "weight is" +dogs[maxindex].getweight ()); }}classDog {PrivateString name; Private floatweight; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public floatgetweight () {returnweight; } Public voidSetweight (floatweight) { This. Weight =weight; }}
Array Summary
1, the array can hold the same type of data;
2, simple data type (int,float) array, can be directly assigned value;
3, after the object array is defined, the assignment needs to allocate space for each object again;
4, the size of the array must be specified beforehand;
5. The array name can be interpreted as a reference to perform the first address of the array;
6. The subscript of the array is numbered starting from 0.
Java from getting started to mastering 06-arrays