自动矩阵创建器(Java)

huangapple go评论64阅读模式
英文:

Automatic matrix creator on Java

问题

这是你的代码的翻译部分:

我想要创建二维矩阵根据输入进行自动化操作然后使它们返回以查看结果

这是我的代码

```java
public class Two_dimensional_arrays {

    static int[][] array1;

    public static int[][] create_array(int number1, int number2) {
        int k = 1;
        for (int j = 0; j <= number2; j++) {
            for (int i = 0; i <= number1; i++) {

                array[i][j] = k;
                k++;
            }
        }

        return array1;
    }
}

所有这一切背后的逻辑是以顺序填充任何矩阵,例如(让我们创建一个3x3的矩阵)

1-2-3

4-5-6

7-8-9

因此,矩阵会自动以升序填充,但由于我对编程还比较新,所以结果并不如预期。


<details>
<summary>英文:</summary>

I wanted to create two-dimensional matrixes on an automated routine (depending on inputs) and then make them return to see the results. 

Here&#39;s my code:

```java
public class Two_dimensional_arrays {

    static int[][] array1;

    public static int[][] create_array(int number1, int number2) {
        int k = 1;
        for (int j = 0; j &lt;= number2; j++) {
            for (int i = 0; i &lt;= number1; i++) {

                array[i][j] = k;
                k++;
            }
        }

        return array1;
    }
}

The logic behind all of that was to fill any matrix in order like (let's make a 3x3 one)

1-2-3

4-5-6

7-8-9

So the matrix would be automatically filled (in ascending order), but it didn't work out as expected since I'm kind of new to programming.

答案1

得分: 2

你必须在某处实际分配数组。

 static int[][] array1;

这声明了一个变量,可以容纳对数组的引用;它并不是数组本身。数组是通过'new'来分配的:

 static int[][] array1 = new int[3][3];

还有一些其他要点:

  1. 循环中的'array'可能应该改为'array1'。

  2. 由于'array1'是成员变量,因此可能没有必要将其作为返回值。你可以选择以下任一方式:保持单个数组,如目前所示,然后每次都使用create_array方法进行覆盖;或者让create_array分配一个预期大小的数组(new int[number1][number2])并将其返回。我会选择后者。

  3. 参数的命名可以更好:'row_count' 而不是 'number1','column_count' 而不是 'number2' - 或者使用任何传达预期含义的类似名称。(通常,Java程序员使用驼峰命名法,如rowCount,而不是下划线,但这不是一个特别有趣的讨论,也不是我在这里要点的内容)。

英文:

You have to actually allocate the array somewhere.

 static int[][] array1;

This declares a variable capable of holding a reference to an array; it is not the array. The array is allocated by 'new':

 static int[][] array1 = new int[3][3];

A couple of other points:

  1. The 'array' in your loop should probably be 'array1'.

  2. Since 'array1' is a member variable there's probably no point in returning it as well. You could go either way: have a single array, as at present, and your create_array method will overwrite it each time; OR have create_array allocate an array of the intended size (new int[number1][number2]) and return it. I'd go for the latter.

  3. The arguments could be named better: 'row_count' rather than 'number1', 'column_count' rather than 'number2' -- or anything similar that conveys the intended meaning. (Typically Java programmers use camel-case names, like rowCount, rather than underscore, but that's not a particularly interesting discussion and not my point here).

答案2

得分: 0

public class Two_dimensional_arrays {
    
    public static int[][] create_array(int number1, int number2){
        int[][] matrix = new int[number1][number2];
        
        int k = 1;
        
        for(int i = 0; i < number1; i++){
            for(int j = 0; j < number2; j++){
                matrix[i][j] = k;
                k++;
            }
        }
        
        return matrix;
    }
    
}
英文:
public class Two_dimensional_arrays {
    
    public static int[][] create_array(int number1, int number2){
        int[][] matrix = new int[number1][number2];
        
        int k = 1;
        
        for(int i = 0; i&lt;number1; i++){
            for(int j = 0; j&lt;number2; j++){
                matrix[i][j] = k;
                k++;
            }
        }
        
        return matrix;
    }
    
}

huangapple
  • 本文由 发表于 2020年8月26日 03:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63585531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定