java可以同时返回两个集合_返回两个集合的交集的具有不同签名的方法(Java)

我创建了一个返回两组值的交集的方法.问题是我想使用一个不同的签名,该签名仅在方法中使用一个arrayList而不是全部.

public class Group

{

ArrayList w = new ArrayList();

//Here I have the add and remove methods and a method that returns

//false if the item is not in the set and true if it is in the set

public static ArrayList intersection(Group A, Group B)

{

ArrayList list = new ArrayList();

ArrayList first = (ArrayList) A.w.clone();

ArrayList second = (ArrayList) B.w.clone();

for (int i = 0; i < A.w.size(); i++)

{

if (second.contains(A.w.get(i)))

{

list.add(A.w.get(i));

second.remove(A.w.get(i));

first.remove(A.w.get(i));

}

}

return list;

}

}

这是具有不同签名的另一种方法.如果签名与上面显示的方法不同,如何使此方法返回两个集合的交集?

public class Group

{

ArrayList w = new ArrayList();

public static Group intersection(Group A, Group B)

{

Group k= new Group();

return k;

}

}

public class Main

{

public static void main(String [] args)

{

Group a1 = new Group();

Group b1 = new Group();

Group a1b1 = new Group();

//Here I have more codes for input/output

}

}

解决方法:

您不能通过Java中它们的返回值来重载方法-您必须重命名其中之一.例如:

public static Group intersectionGroup(Group A, Group B)

public static ArrayList intersectionArrayList(Group A, Group B)

标签:arraylist,java

来源: https://codeday.me/bug/20191121/2049739.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值