我创建了一个返回两组值的交集的方法.问题是我想使用一个不同的签名,该签名仅在方法中使用一个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