Java defines several ways to define two-dimensional arrays

Source: Internet
Author: User

Defining a two-dimensional array notation 1

Class Numthree
{
public static void Main (string[] args)
{
Float[][] numthree;//defines a 2-dimensional array of type float
Numthree=new float[5][5];//allocates 5 rows and 5 columns of space


numthree[0][0]=1.1f;//through the subscript index to access 1 rows and 1 columns =1.1
numthree[1][0]=1.2f;//2 rows 1 Columns =1.2
numthree[2][0]=1.3f;//3 rows 1 Columns =1.3
numthree[3][0]=1.4f;//4 rows 1 Columns =1.4
numthree[4][0]=1.5f;//5 rows 1 Columns =1.5
System.out.println (Numthree[0][0]); Print line break output

}
}

Define a two-dimensional array to define the amount of space allocated at the same time as 2
Class Numfour
{
public static void Main (string[] args)
{
Short[][] Numfour=new short[5][8]; Defines an array of type short and assigns it a space size of 5 rows and 8 columns at the same time
numfour[0][7]=10;
numfour[1][6]=20;
numfour[2][5]=30;
numfour[3][4]=40;
numfour[4][3]=50;
}
}


//defines a two-dimensional array 3 irregular array
class numfive
{
     public static void Main (string[] args)
     {
               long[][] Numfive=new long[5][];//defines a long type of irregular array
                numfive[0]=new long[5];//Assign 5 columns to line 1th
                numfive[1]=new long[6];//Assign 6 columns to line 2nd
               numfive[2]=new long[7];// Assign 7 columns to line 3rd
               numfive[3]=new long[8];//assigning 8 columns to line 4th
               Numfive[4]=new long[9];//Assign 9 columns to line 5th

NUMFIVE[0][4]=10000000000L;//1 Row 5 Column =10000000000

System.out.println (Numfive[0][4]); Print line break output

Print output An array that does not have an array element defined by Java will automatically initialize him to a value of 0
System.out.println (Numfive[4][7]);

     }
}


Defines a 2-D array that assigns an initial value to the 4 definition
Class Numsix
{
public static void Main (string[] args)
{

Defines an array of type Double assigns 3 rows and 3 columns of space at the same time to assign values

Double[][] numsix={{1.111d,2.222d,3.333d},{4.444d,5.555d,6.666d}};

System.out.println (Numsix[0][0]); Print line break output 1 rows 1 columns =1.111
System.out.println (numsix[1][1]); Print line break output 2 rows 2 columns =5.555
}
}


Define a 2-D array 5 define an irregular 2-dimensional array and assign an initial value
Class Numseven
{
public static void Main (string[] args)
{

There's nothing to say if you don't understand it then don't learn it!

     int[][] numseven=new int[][]{{10,20,30},{40,50},{60}};
     System.out.println (numseven[0][2]);
     System.out.println (numseven[1][1]);
     System.out.println (numseven[0][0]);
 }
}

Define a 2-dimensional array 6 define an irregular 2-dimensional array and assign the initial value;
Class Numeight
{
public static void Main (string[] args)
{
Int[][] numeight={{100,200,300,400},{500,600,700,800},{900,1000,1100,1200,1300}};
System.out.println (numeight[0][2]);
System.out.println (numeight[1][2]);
System.out.println (numeight[2][1]);
}
}

This article is from the "Java Stuff" blog, so be sure to keep this source http://1027187712.blog.51cto.com/5509347/1629095

Java defines several ways to define two-dimensional arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.