Java from getting started to mastering 06-arrays

Source: Internet
Author: User
Tags readline

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.