An array of re-learning (one-dimensional, two-dimensional, three-dimensional)

In Java, depending on the type of data belongs, the array is divided into: basic types of data type array and an array of objects
Java array as an object

primitive data type array:
character array: 'A', 'D' , 'q ', ...
××× array: 123, 456, -546
real array: 2.12,3.14
objects array:
String array: "abc", new ( " def"), " Zhao", ...

First, an array of basic data types

int [] a; // declare a reference variable
a = new int [7]; // create an array ××× elements 7, and the created array object is assigned to a reference variable
// i.e. a reference variable references the array
or int [] a = new int [ 7];
and create the object as Class1 ob = new Class1 ();

1 / access array elements, to be used: an array name [index element] is

a [0] = 10; // the array 10 is assigned a first NEUTRONIUM
b = a [3]; // the No. 3 to the variable element of the array B
2 / Example: using an array of four courses seek and the average score

public class Array1 {
    public static void main(String[] args){
        int[] grade=new int[4];
        int sum=0;
        grade[0]=90;
        grade[1]=80;
        grade[2]=75;
        grade[3]=85;
        for(int i=0;i<grade.length;i++){
            sum+=grade[i];
        }
        System.out.println("总分:"+sum);
        System.out.println("平均分:"+(double)sum/grade.length);
    }
}

Second, the one-dimensional array initialization

Basic data type initialization

int[] a=new int[]{1,2,3,4}
int[] b={6,7,8,9}

Object array initialization

String[] s1=new String[]{new String("你好"),new String ("高兴")};
String[] s2={new String("abc"),new String("kor")};

If you create an array without initializing, the basic data type of the array is automatically assigned to 0, the object is null array Fu

Third, the array of objects referenced object array // // Array is actually a collection of objects referenced array

String [] S; // declare a reference variable array of String
s = new String [3]; // creates an array with 3 elements, the array and the reference object is assigned to the variable S
// array of basic type an entire array of a considered a reference, it can not create object to each element of
// a reference may correspond to an object, the object may be a plurality of reference points to
s [0] = new String ( " Zhao"); // object in the array is a reference to all the elements, it is possible to create an object for each element of
s [1] = new String ( " Louis Koo");
S [2] = new new String ( "hot bar");

Fourth, the two-dimensional array

An array of basic data types

int[][] a;//声明a是整型二维数组的引用(变量)
a=new int[3][];//创建3个一维数组的引用a[0],a[1],a[2]
a[0]=new int[5];//给一维数组的引用a[0],a[1],a[2]分配对象
a[1]=new int[4];
a[2]=new int[3];
a[0][0]=10;给a[0]的第一个元素赋值

Reference array

String[][] s=new String[2][];
s[0]=new String[2];
s[1]=new String[2];
s[0][0]="中国";s[0][1]="China";
s[1][0]="美国";s[1][1]="USA";

An array of re-learning (one-dimensional, two-dimensional, three-dimensional)

Five, three-dimensional array

int[][][] a=new int[2][][];//表示创建二维数组的引用数组a[0],a[1]
a[0]=new int[2][];//将二维数组的引用赋给一维数组的引用数组,a[0][0],a[0][1]
a[1]=new int[3][];//同上创建3个一维数组的引用
a[ 0][ 0]  =new int[4];//创建一个拥有4个元素的整型数组,
                                    //a[0][0]a引用它,数组元素为a[0][0][0],a[0][0][1],a[0][0][2],a[0][0][3]
a[0][1]=new int[4];//其他行类似
a[1][0]=new int[3];
a[1][1]=new int[3];
a[1][2]=new int[3];

An array of re-learning (one-dimensional, two-dimensional, three-dimensional)

Sixth, pay attention:

int[][] a,b;//a和b 都是一维数组的引用
int a[],b[];//b是二维数组的引用
int a[],b;//b是整型变量而非数组
随机数产生:
double a=Math.random();//a拥有0.0~1.0的值

Guess you like

Origin blog.51cto.com/14234228/2424893