Java如何定义一个二维数组

  1. package com.itmyhome;  
  2.   
  3. public class T {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.         //较为普遍使用最多的方式  
  11.         float f1[][] = new float[3][3];  
  12.           
  13.         //对象名称f2可放在[][]前面、后面或中间  
  14.         float []f2[] = new float[3][3];  
  15.           
  16.         //测试打印f2,可正常使用   
  17.         for(int i=0;i<f2.length;i++){  
  18.             for(int j = 0;j<f2[i].length;j++){  
  19.                 System.out.println(f2[i][j]);  
  20.             }  
  21.             System.out.println();  
  22.         }  
  23.         //二维数组可只定义行,而不定义列,反之则不然  
  24.         float f3[][] = new float[][3];  //此种方法错误  
  25.         float [][]f4 = new float[3][3];  
  26.         float [][]f5 = new float[3][];  //可以只指定行 而不指定列  
  27.     }  
  28. }  

猜你喜欢

转载自blog.csdn.net/qq_18972767/article/details/78488960