Several ways of writing Java-defined two-dimensional arrays (summary) _java

Source: Internet
Author: User

As shown below:


Class Numthree
{public
static void Main (string[] args)
{
float[][] numthree;       Defines a float-type 2-d array
numthree=new float[5][5];    Allocate 5 rows of 5 columns of space to size
numthree[0][0]=1.1f;      Access to   1 rows of 1 columns of =1.1 numthree[1][0]=1.2f by subscript index
;                 2 rows of 1 columns =1.2
numthree[2][0]=1.3f;                 3 Rows of 1 columns =1.3
numthree[3][0]=1.4f;                 4 rows of 1 columns =1.4
numthree[4][0]=1.5f;                 5 rows of 1 columns =1.5
System.out.println (numthree[0][0]);//print line-wrapping output
System.out.println (numthree[1][0));
System.out.println (Numthree[2][0]);
System.out.println (Numthree[3][0]);
System.out.println (Numthree[4][0]);
}
//definition of two-dimensional array notation 2 definition of concurrent allocation space size class Numfour {public static void main (string[] args) {Shor T[][] Numfour=new short[5][8];
  Defines a short type of array and assigns it 5 rows of 8 columns of space size numfour[0][7]=10;
  numfour[1][6]=20;
  numfour[2][5]=30;
  numfour[3][4]=40;
  numfour[4][3]=50;
  System.out.println (Numfour[0][7]);
  System.out.println (Numfour[1][6]);
  System.out.println (Numfour[2][5]);
  System.out.println (Numfour[3][4]);
System.out.println (Numfour[4][3]); }
}
//definition two-dimensional array notation 3 irregular array class Numfive {public static void main (string[] args) {long[][] n   Umfive=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];       Assign 8 columns to line 4th numfive[4]=new Long[9];     Allocate 9 column numfive[0][4]=10000000000l to line 5th;     1 rows of 5 columns =10000000000 numfive[1][5]=20000000000l;     2 rows of 6 columns =20000000000 numfive[2][6]=30000000000l;     3 Rows of 7 columns =30000000000 numfive[3][7]=40000000000l;     4 rows of 8 columns =40000000000 numfive[4][8]=50000000000l; 5 Rows of 9 columns =50000000000 System.out.println (numfive[0][4]);
Print line-Wrapping output System.out.println (numfive[1][5]);
System.out.println (Numfive[2][6]);
System.out.println (Numfive[3][7]);
System.out.println (Numfive[4][8]); System.out.println (Numfive[4][7]); PrintOut An array Java that does not define an array element will automatically initialize the value to 0}} 
To define the 2-D array notation 4  , assign the initial value
class Numsix
{public
static void Main (string[] args)
{
double[][] numsix={{1.111d,2.222d,3.333d},{4.444d,5.555d,6.666d}};//defines a double array allocating 3 rows and 3 columns of space and assigning
System.out.println ( Numsix[0][0]); Print line wrap output 1 rows 1 columns =1.111
System.out.println (numsix[1][1]);//print line-break output 2 rows 2 columns =5.555
}
}
Define 2-D array notation 5  define an irregular 2-D array and assign the initial value
class Numseven
{public
static void Main (string[] args)
{
int[][] numseven=new int[][]{{10,20,30},{40,50},{60};//There's nothing to talk about. If you don't understand, then don't learn!
System.out.println (numseven[0][2]);
System.out.println (numseven[1][1]);
System.out.println (Numseven[0][0]);
}
Define 2-D array notation 6 define an irregular 2-D 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]);
}

The above is a small series for you to bring the Java definition of two-dimensional array of several ways to write (summary) of the full content, I hope that we support the cloud Habitat Community ~

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.