数组

package com.company;

import java.util.Arrays;

public class Main {

public static void main(String[] args) {

// 一维数组

// 第一种方式

int a[] = {1,2,3};

System.out.println(Arrays.toString(a));

//第二种方式

int [] b = new int[3];

//        b={1,2,3};

b[0] = 1 ; b[1] = 2 ; b[2] =3;

int c, d;

c = 1 ;

d = 2;

// 第三种方式

int [] e = new int[] {1,2,3};

// 二维数组

int temp [][] = new int[2][3];

temp[0][0] = 1;

temp[0][1] = 2;

temp[0][2] = 3;

temp[1][0] = 4;

temp[1][1] = 5;

temp[1][2] = 6;

System.out.println(Arrays.toString(temp));

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 3; j++) {

System.out.print(temp[i][j]  + "\t");

}

System.out.println();

}

for (int i = 0; i < temp.length; i++) {

for (int j = 0; j < temp[i].length; j++) {

System.out.print(temp[i][j]  + "\t");

}

System.out.println();

}

// 二维数组

int [][] bArray = new int[4][5] ;

int temp1 = 1 ;

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 5; j++) {

bArray[i][j] = temp1++;

}

}

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 5; j++) {

System.out.print(bArray[i][j] + "\t ");

}

System.out.println();

}

// 二维数组

int [] [] cArray = new int[][] { {1,2,3},{4,5,6} };

for (int i = 0; i < cArray.length; i++) {

for (int j = 0; j < cArray[i].length; j++) {

System.out.print( cArray[i][j] +"\t");

}

System.out.println();

}

}

}

你可能感兴趣的:(数组)