在Java中,int[] a和int a[] 的区别

1,752 阅读3分钟

在Java中int[] a和int a[] 有什么区别吗?

Java中的数组是一组类型相同的变量,由一个共同的名称来指代。Java中的数组与C/C++中的数组工作方式不同。 在Java中,数组可以用以下方式声明。

一维数组。一维数组声明的一般形式是

type var-name[];
OR
type[] var-name。

多维数组。

int[][] intArray = new int[10][20]; //二维数组或矩阵
int[][] intArray = new int[10][20][10]; //三维数组

对于Java中的一维数组,"int[] a "和 "int a[] "之间的区别

对于一维数组,在Java中没有区别,任何一种提到的语法都可以用来声明一个一维数组。

比如说。

Java

// Java program to illustrate creating an array
// of integers, puts some values in the array,
// and prints each value to standard output.

class GFG {
	public static void main(String[] args)
	{
		// declares an Array of integers
		// using method 1
		int[] arr1;
		arr1 = new int[5];
		arr1[0] = 10;
		arr1[1] = 20;
		arr1[2] = 30;
		arr1[3] = 40;
		arr1[4] = 50;

		// accessing the elements
		// of the specified array
		for (int i = 0; i < arr1.length; i++)
			System.out.println("Array from method 1: "
							+ arr1[i]);
		System.out.println();

		// declares an Array of integers
		// using method 2
		int arr2[];
		arr2 = new int[5];
		arr2[0] = 1;
		arr2[1] = 2;
		arr2[2] = 3;
		arr2[3] = 4;
		arr2[4] = 5;

		// accessing the elements
		// of the specified array
		for (int i = 0; i < arr2.length; i++)
			System.out.println("Array from method 2: "
							+ arr2[i]);
	}
}

输出

Array from method 1: 10
Array from method 1: 20
Array from method 1: 30
Array from method 1: 40
Array from method 1: 50

Array from method 2: 1
Array from method 2: 2
Array from method 2: 3
Array from method 2: 4
Array from method 2: 5

在Java中声明多个数组时,"int[] a "和 "int a[] "之间的区别

在Java中同时声明多个数组时,声明的方法很重要,需要遵循正确的语法。如果不这样做,将导致编译时的错误。

  • 声明多个数组的正确语法

    int []a, b;
    

    比如说。

    Java

// Program to show array declaration using int[] a, b syntax

import java.io.*;

class GFG {
   public static void main(String[] args)
   {
   	int[] a, b;
   	// Here both a and b are integer arrays
   	a = new int[3];
   	b = new int[4];
   	System.out.print("array a: ");
   	for (int i = 0; i < 3; i++) {
   		a[i] = i;
   		System.out.print(a[i] + " ");
   	}
   	System.out.print("\n");
   	System.out.print("array b: ");

   	for (int i = 0; i < 4; i++) {
   		b[i] = i;
   		System.out.print(b[i] + " ");
   	}
   }
}
**输出**

```
array a: 0 1 2 
array b: 0 1 2 3 
```
  • 声明多个数组的正确语法

    int a[], b;
    int a, b[];
    

    例1:显示int a[], b声明的输出的例子。

    在Java中使用int a[], b方法声明多个数组时,编译器会将 "a "声明为一个数组,而 "b "将被声明为一个整数变量。因此,在访问时,会产生一个编译器错误。

    --

// Program to show array declaration
// using int a[], b syntax

import java.io.*;

class GFG {
	public static void main(String[] args)
	{

		// While using this method to declare
		// multiple arrays in Java,
		// the compiler will declare a as an array,
		// whereas b will be declared
		// as an integer variable
		// Hence while accessing this
		// will give compiler error
		int a[], b;

		b = new int[4];
		a = new int[3];
		System.out.print("array a: ");
		for (int i = 0; i < 3; i++) {
			a[i] = i;
			System.out.print(a[i] + " ");
		}
		System.out.print("\n");
		System.out.print("array b: ");

		for (int i = 0; i < 4; i++) {
			b[i] = i;
			System.out.print(b[i] + " ");
		}
	}
}
**编译时的错误信息。**

```
prog.java:19: error: incompatible types: int[] cannot be converted to int
        b = new int[4];
            ^
prog.java:30: error: array required, but int found
            b[i] = i;
             ^
prog.java:31: error: array required, but int found
            System.out.print(b[i] + " ");
                              ^
3 errors
```

**例2:**显示**int a, b\[\]**声明输出的例子。

> 在Java中使用**int a, b\[\]**方法声明多个数组时,编译器会将 "a "声明为一个整数变量,而 "b "会被声明为一个整数数组。因此,在访问时,会出现编译器错误。

--
// Program to show array declaration
// using int a, b[] syntax

import java.io.*;

class GFG {
  public static void main(String[] args)
  {

  	// While using this method to declare
  	// multiple arrays in Java,
  	// the compiler will declare a as an array,
  	// whereas b will be declared
  	// as an integer variable
  	// Hence while accessing this
  	// will give compiler error
  	int a, b[];

  	b = new int[4];
  	a = new int[3];
  	System.out.print("array a: ");
  	for (int i = 0; i < 3; i++) {
  		a[i] = i;
  		System.out.print(a[i] + " ");
  	}
  	System.out.print("\n");
  	System.out.print("array b: ");

  	for (int i = 0; i < 4; i++) {
  		b[i] = i;
  		System.out.print(b[i] + " ");
  	}
  }
}

Java中多维数组的 "int[] a "和 "int a[]"之间的区别

对于多维数组,在Java中没有区别,可以使用上述任何一种语法进行声明。

例如。

Java

public class multiDimensional {
	public static void main(String args[])
	{
		// Syntax 1
		// declaring and initializing 2D array
		int[][] arr1
			= { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };

		// printing 2D array
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++)
				System.out.print(arr1[i][j] + " ");

			System.out.println();
		}
		System.out.println();

		// Syntax 2
		// declaring and initializing 2D array
		int arr2[][] = { { 10, 20, 30 }, { 40, 50, 60 } };

		// printing 2D array
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 3; j++)
				System.out.print(arr2[i][j] + " ");

			System.out.println();
		}
	}
}

输出

2 7 9 
3 6 1 
7 4 2 

10 20 30 
40 50 60 

在声明数组时,"int[] a "和 "int a[]"哪种语法更可取?

  • 虽然,这两种声明方式在功能上没有区别。两者都声明一个整数数组,因此,没有结论说哪种风格更可取,int[] a是Java中声明数组的首选语法,而**int a[]**则是为了帮助传统的C/C++程序员。
  • 从技术上讲,在声明一个数组的情况下,两种语法是一样的。但是如果我们在一条语句中声明多个数组,这两种声明方式会产生不同的结果。