Java中的数组声明(单个和多维)

Java中的数组|简介

null

一维数组:

它是由同一类型的变量组成的集合,这些变量由一个公共名称使用。

例如: 变量的一维数组声明:

import java.io.*;
class GFG {
public static void main(String[] args)
{
int [] a; // valid declaration
int b[]; // valid declaration
int [] c; // valid declaration
}
}


我们可以用任何方式写。

现在,如果您像下面这样声明数组:

import java.io.*;
class GFG {
public static void main(String[] args)
{
// invalid declaration -- If we want to assign
// size of array at the declaration time,  it
// gives compile time error.
int a[ 5 ];
// valid declaration
int b[];
}
}


现在,假设我们想要编写数组变量的多个声明,那么我们可以这样使用它。

import java.io.*;
class GFG {
public static void main(String[] args)
{
// valid declaration, both arrays are
// one dimensional array.
int a[], b[];
// invalid declaration
int c[], [] d;
// invalid declaration
int [] e, [] f;
}
}


当我们同时声明多个变量时,我们必须先写变量,然后声明该变量,除了第一个变量声明。第一个变量没有限制。

现在,当我们创建数组时,必须传递数组的大小;否则我们将得到编译时错误。 可以使用new运算符创建数组。

import java.io.*;
class GFG {
public static void main(String[] args)
{
// invalid, here size of array is not given
int [] a = new int [];
// valid, here creating 'b' array of size 5
int [] b = new int [ 5 ];
// valid
int [] c = new int [ 0 ];
// gives runtime error
int [] d = new int [- 1 ];
}
}


打印阵列:

/* A complete Java program to demonstrate working
of one dimensional arrays */
class oneDimensionalArray {
public static void main(String args[])
{
int [] a; // one dimensional array declaration
a = new int [ 3 ]; // creating array of size 3
for ( int i = 0 ; i < 3 ; i++) {
a[i] = 100 ;
System.out.println(a[i]);
}
}
}


输出:

100
100
100

二维阵列

假设您想要创建int类型数据的二维数组。因此,可以通过以下多种方式声明二维数组:

// Java program to demonstrate different ways
// to create two dimensional array.
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a[][]; // valid
int [][] b; // valid
int [][] c; // valid
int [] d[]; // valid
int [][] e; // valid
int [] f[]; // valid
[][] int g; // invalid
[] int [] h; // invalid
}
}


现在,假设我们想要编写数组变量的多个声明,那么您可以这样使用它。

// Java program to demonstrate multiple declarations
// of array variable
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Here, 'a' is two dimensional array, 'b'
// is two dimensional array
int [] a[], b[];
// Here, 'c' is two dimensional array, 'd'
// is two dimensional array
int [] c[], d[];
// Here, 'e' is two dimensional array, 'f'
// is three dimensional array
int [][] e, f[];
// Here, 'g' is two dimensional array,
// 'h' is one dimensional array
int [] g[], h;
}
}


在不使用新运算符的情况下创建一维数组和二维数组:

/* Java program for one and two dimensional arrays.
without new operator*/
class oneTwoDimensionalArray {
public static void main(String args[])
{
int [] a[] = { { 1 , 1 , 1 }, { 2 , 2 , 2 },
{ 3 , 3 , 3 } }, b = { 20 };
// print 1D array
System.out.println(b[ 0 ]);
// print 2D array
for ( int i = 0 ; i < 3 ; i++) {
for ( int j = 0 ; j < 3 ; j++) {
a[i][j] = 100 ;
System.out.println(a[i][j]);
}
}
}
}


输出:

20
100
100
100
100
100
100
100
100
100

使用新运算符创建一维数组和二维数组:

/* Java program for one and two dimensional arrays.
using new operator*/
class oneTwoDimensionalArray {
public static void main(String args[])
{
int [] a[], b = { 20 };
a = new int [ 3 ][ 3 ];
b = new int [ 3 ];
// print 1D array
for ( int i = 0 ; i < 3 ; i++)
System.out.println(b[i]);
// print 2D array
for ( int i = 0 ; i < 3 ; i++) {
for ( int j = 0 ; j < 3 ; j++) {
a[i][j] = 100 ;
System.out.println(a[i][j]);
}
}
}
}


输出:

0
0
0
100
100
100
100
100
100
100
100
100

本文由 闪烁帕特尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享