数组array

数组array
数组的结构: 数据类型[] 变量名 = 赋值;
数组的三种声明方式
    String[] array = { "U", "W", "H" };// 直接赋值
    String[] array2 = new String[4];// 定义长度
    // 通过new的方式赋值
    String[] array3 = new String[] { "A", "B", "C" };

实例
package fourcircle;
public class TwoDimensionArray {
    public static void main(String[] args) {
       int[][] arr = new int[3][5];
       // for循环遍历二维数组
       for (int i = 0; i < arr.length; i++) {
           for (int j = 0; j < arr[i].length; j++) {
               int element = (int) (Math.random() * 10);// 强制类型转换,随机产生[0,10)之间的整数
               arr[i][j] = element;
               System.out.print(arr[i][j]+"\t");
           }
           System.out.println();
       }
        System.out.println("--------------------华丽丽滴分割线-------------------------");
       // foreach
       for (int[] i : arr) {
           for (int j : i) {
               int a = (int) (Math.random() * 20);
               System.out.print(a + "\t");
           }
           System.out.println();
       }
        System.out.println("--------------------华丽丽滴分割线-------------------------");
       /* while遍历二维数组 */
       int i = 0;
       while (i < arr.length) {
           int j = 0;
           while (j < arr[i].length) {
               int a = (int) (Math.random() * 17);
               arr[i][j] = a;
               System.out.print(arr[i][j] + "\t");
               j++;
           }
           i++;
           System.out.println();
       }
        System.out.println("--------------------华丽丽滴分割线-------------------------");
       /* do-while遍历二维数组 */
       int j = 0;
       do {
           int k = 0;
           do {
               arr[j][k] = (int) (Math.random() * 37);
               System.out.print(arr[j][k] + "\t");
               k++;
           } while (k < arr[j].length);
           System.out.println();
           j++;
       } while (j < arr.length);
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页