有没有办法从java中的一个方法传回两种不同的数据类型?

jslywgbw  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(273)

我试图用有序的数字填充第一列中的空数组,并将其写回。这是可行的,但我也需要传回行位置,以便在另一个方法中引用。

// Generate a customer/order number.
public static String[][] gen_cust_number(String[][] cust_order, int order_location)
{
    for(int row = 0; row < cust_order.length; row++)
    {
        if (cust_order[row][0] == null)
        {
            cust_order[row][0] = Integer.toString(row + 1000);  
            order_location = row;
            Gen.p("\n\n\tYour order number is : " + cust_order[row][0]);
            break;
        }
    }
    return cust_order;
}

我不是很熟悉的对象,对工作,以及什么,因为我仍然在学习,但已经做了一些搜索,并在了解如何做它难倒。

eoxn13cs

eoxn13cs1#

我不是100%确定你想要达到什么,但是通过阅读代码我想什么 get_cust_number 应该做的是
为第一个空订单列表生成新订单。
返回新的订单列表及其索引。
如果这是正确的,你不必通过考试 String[][] 因为这个示例的引用是调用方在参数中传递时已经知道的。
也可以删除 order_location param,因为它从未在方法内部读取。
所以你能做的就是
拆下 order_location 从params。
返回添加顺序的索引,而不是数组本身。
这将产生以下代码。

// Generate a customer/order number.
public static int gen_cust_number(String[][] cust_order)
{
    for(int row = 0; row < cust_order.length; row++)
    {
        if (cust_order[row][0] == null)
        {
            cust_order[row][0] = Integer.toString(row + 1000);  
            Gen.p("\n\n\tYour order number is : " + cust_order[row][0]);
            return row;
        }
    }
    // cust_order is full
    return -1;
}

在呼叫端,您可以执行以下操作:

String[][] cust_order = /* Some orders according to your logic. */;
int cust_order_count = /* Number of the orders generated. */;

// Generate the order and this will be the new number of orders.
cust_order_count = gen_cust_number(cust_order);

相关问题