首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

二维数组在Java跟C中的区别

2012-08-31 
二维数组在Java和C中的区别Java代码:?public class test03 {public static void main(String[] args) {int

二维数组在Java和C中的区别

Java代码:

?

public class test03 {public static void main(String[] args) {int[][] a = {{1},{2,3},{4,5,6}};System.out.println(a[0][1]);}}

?运行结果:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
??? at com.java.test03.main(test03.java:7)

?

C语言代码:

#include <conio.h>#include <stdio.h>   void main(){   int a[3][3] = {{1},{2,3},{4,5,6}};printf("%d", a[0][1]);printf("\n");system("pause");}

?运行结果:

0

?

Java实际上没有多维数组,只有一维数组。多维数组被解释为“数组的数组”。

?

?

Java定义数组可以写:int[][] a = new int[3][];

C语言定义数组可以写:int a[][3] = {{1},{2,3},{4,5,6}};

热点排行