Difference between int[] array and int array[]

前端 未结 25 3553
轻奢々
轻奢々 2020-11-21 07:18

I have recently been thinking about the difference between the two ways of defining an array:

  1. int[] array
  2. int array[]
  3. <
25条回答
  •  爱一瞬间的悲伤
    2020-11-21 08:11

    As already stated, there's no much difference (if you declare only one variable per line).

    Note that SonarQube treats your second case as a minor code smell:

    Array designators "[]" should be on the type, not the variable (squid:S1197)

    Array designators should always be located on the type for better code readability. Otherwise, developers must look both at the type and the variable name to know whether or not a variable is an array.

    Noncompliant Code Example

    int matrix[][];   // Noncompliant
    int[] matrix[];   // Noncompliant
    

    Compliant Solution

    int[][] matrix;   // Compliant
    

提交回复
热议问题