gpt4 book ai didi

java - Eclipse (Java) 中未找到源错误

转载 作者:行者123 更新时间:2023-12-02 08:07:05 24 4
gpt4 key购买 nike

我有这几行代码:

Set<Group> setofAllGroups;
setofAllGroups = new TreeSet<Group>();
Group[] allGroupsArray = (Group[]) setofAllGroups.toArray();

最后一行导致运行时错误,在 Debug模式下我得到“找不到源”

Group 类的代码:

public class Group 
{
String groupName;
Set<Recipient> groupMembers;

public Group()
{
groupName = "";
groupMembers = new TreeSet<Recipient>();
}

public void setGroupName(String name)
{
groupName = name;
return;
}

public void addMember(Recipient toAdd)
{
groupMembers.add(toAdd);
return;
}
public void addMember(String name, String phoneNumber)
{
Recipient toAdd = new Recipient(name, phoneNumber);
groupMembers.add(toAdd);
return;
}
public void removeMember(Recipient toRemove)
{
groupMembers.remove(toRemove);
}
public void removeMember(String name, String phoneNumber)
{
Recipient toRemove = new Recipient(name, phoneNumber);
groupMembers.remove(toRemove);
}
public void removeAllGroupMembers()
{
groupMembers.clear();
}

}

运行时错误的原因是什么?

最佳答案

快速回答是你不能这样转换:

Group[] allGroupsArray = (Group[]) setofAllGroups.toArray();

相反,你必须使用这个:

Group[] allGroupsArray = setofAllGroups.toArray(
new Group[setofAllGroups.size()]);

这是因为 toArray() 返回 Object[],然后您尝试将 Object[] 转换为 Group[]。这失败了。

关于java - Eclipse (Java) 中未找到源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7999747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com