Java array definition and initialization +print printing problems

Source: Internet
Author: User
Tags array definition

General Write SyStem.out.pirntln (xxxxx); This is the default line break equals after printing by default added:/n/r, if you want to output on the same line, then write: system.out.print (xxx);

Defined:
int a[]; Declaration not initialized
A = new int [10]; Define the size of the footprint (10 Int)
int a[] = new int [10]; Declare and define size (that is, space assigned the specified Size)
int a[] = {n/a}; Declares and initializes, with a space size of 3 int.
Initialization
One-dimensional arrays

1)
int[] a; declaration, not initialized

2)
int[] a=new int[5];

3)
int[] a={1,2,3,4,5};
Initialize to the given value
4)
int[] a=new int[]{1,2,3,4,5};

int[] a=new int[5]{1,2,3,4,5};
error, cannot define a dimension expression if an array initialization operation is provided
5)
int[] a;
A=new int[5]; right, Same as (2)

int[] a;

a={1,2,3,4,5}; Error array constants can only be used in initialization operations, such as (3)

6)
a[0]=1;
Error because the array is not initialized and cannot be assigned a value of a[1]=2; Two-dimensional arrays

1)

declaration, not initialized

2) int[][] a=new int[2][3];


3)
int[][] a={{1,2},{2,3},{3,4}};
Initialize to the given value
int[][] a={{1,2},{2,3},{3,4,5}};
No mistake, The array space is not contiguous, so the size of each dimension is not required
4)



The size of each dimension can be different


5)
int[][] a=new
int[][]{{1,2},{2,3},{3,4,5}};
Same (3)




Java array Definition and initialization of +print printing problems

Related Article

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.