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

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

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

  1. // Generate a customer/order number.
  2. public static String[][] gen_cust_number(String[][] cust_order, int order_location)
  3. {
  4. for(int row = 0; row < cust_order.length; row++)
  5. {
  6. if (cust_order[row][0] == null)
  7. {
  8. cust_order[row][0] = Integer.toString(row + 1000);
  9. order_location = row;
  10. Gen.p("\n\n\tYour order number is : " + cust_order[row][0]);
  11. break;
  12. }
  13. }
  14. return cust_order;
  15. }

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

eoxn13cs

eoxn13cs1#

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

  1. // Generate a customer/order number.
  2. public static int gen_cust_number(String[][] cust_order)
  3. {
  4. for(int row = 0; row < cust_order.length; row++)
  5. {
  6. if (cust_order[row][0] == null)
  7. {
  8. cust_order[row][0] = Integer.toString(row + 1000);
  9. Gen.p("\n\n\tYour order number is : " + cust_order[row][0]);
  10. return row;
  11. }
  12. }
  13. // cust_order is full
  14. return -1;
  15. }

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

  1. String[][] cust_order = /* Some orders according to your logic. */;
  2. int cust_order_count = /* Number of the orders generated. */;
  3. // Generate the order and this will be the new number of orders.
  4. cust_order_count = gen_cust_number(cust_order);
展开查看全部

相关问题