zoukankan      html  css  js  c++  java
  • Java中的Jagged数组

    [
  •   Java 数组

    Java中的Jagged数组

    Jagged数组是数组的数组,这样成员数组的大小可以不同,也就是说,我们可以创建一个二维数组,但每行的列数都是可变的。这些类型的数组也被称为Jagged数组。

    以下是展示上述概念的Java程序。

    // Program to demonstrate 2-D jagged array in Java
    class Main
    {
        public static void main(String[] args)
        {
            // Declaring 2-D array with 2 rows
            int arr[][] = new int[2][];
     
            // Making the above array Jagged
     
            // First row has 3 columns
            arr[0] = new int[3];
     
            // Second row has 2 columns
            arr[1] = new int[2];
     
            // Initializing array
            int count = 0;
            for (int i=0; i<arr.length; i++)
                for(int j=0; j<arr[i].length; j++)
                    arr[i][j] = count++;
     
            // Displaying the values of 2D Jagged array
            System.out.println("Contents of 2D Jagged Array");
            for (int i=0; i<arr.length; i++)
            {
                for (int j=0; j<arr[i].length; j++)
                    System.out.print(arr[i][j] + " ");
                System.out.println();
            }
        }
    }
    

    输出:

    Contents of 2D Jagged Array
    0 1 2 
    3 4

    以下是i行有i列的另一个例子,即第一行有1个元素,第二行有两个元素,依此类推。

    // Another Java program to demonstrate 2-D jagged
    // array such that first row has 1 element, second
    // row has two elements and so on.
    class Main
    {
        public static void main(String[] args)
        {
            int r = 5;
     
            // Declaring 2-D array with 5 rows
            int arr[][] = new int[r][];
     
            // Creating a 2D array such that first row
            // has 1 element, second row has two
            // elements and so on.
            for (int i=0; i<arr.length; i++)
                arr[i] = new int[i+1];
     
            // Initializing array
            int count = 0;
            for (int i=0; i<arr.length; i++)
                for(int j=0; j<arr[i].length; j++)
                    arr[i][j] = count++;
     
            // Displaying the values of 2D Jagged array
            System.out.println("Contents of 2D Jagged Array");
            for (int i=0; i<arr.length; i++)
            {
                for (int j=0; j<arr[i].length; j++)
                    System.out.print(arr[i][j] + " ");
                System.out.println();
            }
        }
    }
    

    输出:

    Contents of 2D Jagged Array
    0 
    1 2 
    3 4 5 
    6 7 8 9 
    10 11 12 13 14
  •   Java 数组
    ]
    转载请保留页面地址:https://www.breakyizhan.com/java/4043.html
  • 相关阅读:
    Navicat Premium 12连接Oracle时提示oracle library is not loaded的问题解决
    事务传播机制Propagation.REQUIRES_NEW
    @ApiImplicitParams、ApiImplicitParam的使用
    启动微服务项目的时候报redisInit错误---本地启动redis服务
    Swagger介绍及使用
    mybaitis框架-trim标签
    pgadmin怎样创建新的连接
    微服务项目启动
    管理中第一可怕之事(2) . 分类: 项目管理 2014-06-25 18:54 257人阅读 评论(0) 收藏
    管理中第一可怕之事(1) . 分类: 项目管理 2014-06-25 18:53 264人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/breakyizhan/p/13257671.html
Copyright © 2011-2022 走看看