简体   繁体   English

Java:如何以不同的方法获取数组的长度?

[英]Java: How to get length of array in different method?

A question in my Java textbook: 我的Java教科书中的一个问题:


Complete this class: 完成本课程:

public class MyMatrix
{
    public MyMatrix(int[][] elements)
    {
        // create new matrix with elements as the content
    }
    public int getRows()
    {
        // get the number of rows of the matrix
    }
}

Don't write any code outside the method bodies. 不要在方法主体之外编写任何代码。


This is what I'm trying to do: 这就是我想要做的:

public class MyMatrix
{
    public MyMatrix(int[][] elements)
    {
        int[][] matrix = elements;
    }
    public int getRows()
    {
        return matrix.length;
    }
}

This obviously doesn't work since matrix is local to the constructor. 这显然是行不通的,因为matrix对于构造函数而言是本地的。

I don't see how I can make matrix available to other methods without working outside the method bodies. 我不知道如何在不使用方法主体的情况下使matrix可用于其他方法。 Could someone show me how to do this? 有人可以告诉我该怎么做吗?

The only possible solution to your problem is to tell the "getRows()" method to take the Matrix as an argument. 解决您的问题的唯一可能方法是告诉“ getRows()”方法以Matrix作为参数。

This way, you are not creating code outside of the methods, rather using what you are given. 这样,您不是在方法之外创建代码,而是使用给定的代码。


 public class MyMatrix { public MyMatrix(int[][] elements) { int[][] matrix = elements; int length = getRows(matrix); } public int getRows(int[][] matrix) { return matrix.length; } } 


Hope this answers your question. 希望这能回答您的问题。

Let me know what the outcome is :) 让我知道结果是什么:)

Good luck! 祝好运!

If the text book question is as you stated, then it is a trick question. 如果教科书问题与您所说的一样,那么这是一个技巧问题。

Clearly, if getRows must return the length of the array passed to the constructor, then there must be some information transfer from constructor to the method. 显然,如果getRows必须返回传递给构造函数的数组的长度,那么必须存在一些从构造函数到方法的信息传递。 That means there must be a variable that the constructor directly or indirectly writes and that the method directly or indirectly reads. 这意味着必须有一个变量,构造函数直接或间接写入该变量,并且该方法直接或间接读取该变量。

The best I can come up with is this: 我能想到的最好的方法是:

public class Base {
    int[][] matrix;
    Base(int[][] elements) { matrix = elements; }
    int getRows() { return matrix.length; }
}

public class MyMatrix extends Base { 
    // No code added here
    public MyMatrix(int[][] elements) { super(elements); }
    public int getRows() { return super.getRows(); }
}

Or ... you could write it like this: 或者...您可以这样写:

public class MyMatrix  { 
    // No "code" added outside of the method bodies.  This
    // is a declaration ... not "code".
    private int[][] array;
    public MyMatrix(int[][] elements) { array = elements; }
    public int getRows() { return elements.length; }
}

But these are both "trick" answers ... in that they are relying on sophistry over what "[d]on't write any code outside the method bodies" actually means. 但是,这些都是“技巧”答案……它们依赖于诡辩,即“ [不在方法主体之外编写任何代码]”的实际含义。 In fact, I don't think there is a solution that is not a trick answer in some sense. 实际上,从某种意义上讲,我认为没有解决方案不是绝妙的答案。

Frankly, I doubt the wisdom including trick questions like this in a text book. 坦白说,我怀疑在教科书中是否包含诸如此类的技巧性问题。 The student won't learn how to program from trying (and failing) to solve trick questions! 学生将不会从尝试(和失败)解决技巧问题中学习如何编程!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM