Java learning summary – array creation and parameter transfer

(1) Array creation

The creation of array includes two parts: the declaration of array and the allocation of memory space.

int score[]=null; //申明一维数组score=new int[3]; 分配长度为3的空间

There is another way to declare arrays:

int[] score=把中括号写在数组名前面

Usually, when writing code, we combine two lines into one line for convenience:

int score[3]; 将数组申明与分配内存写在一行

(2) Transfer parameters

In Java, the array passes parameters in the way of address reference, that is, we pass the first address of the array, which can be changed directly. There are three main points:

·The argument is the array name;

·The formal parameter is a newly declared array. If there is a return value, you need to add brackets "[]" after the type of the function;

·The return value is the array name.

Examples are as follows:

/**
 * Created by lijiaman on 2016/9/16. */public class createArray2
{static void main(String[] args)
    {null;
        score=int[3];int speed[]={12,35};for(int x=0;x<3;x++)
        {
            score[x]=x*2+1;
        })
        {
            System.out.println("score["+x+"]="+score[x]);
        }
        System.out.println("length:"+score.length);int x=0;x<speed.length;x++)
        {
            System.out.println("Speed:"+speed);
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>