gpt4 book ai didi

java - 循环数组列表并将与两个数组列表匹配的值添加到新的数组列表中

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:14 25 4
gpt4 key购买 nike

我正在尝试制作一个将客户兴趣与假期的各个方面相匹配的程序,我有一个假期数组列表和一个客户兴趣字符串数组列表,我想创建一个新的数组列表,它将添加两者中包含的所有元素。这就是我所拥有的,但它出现了空指针异常,有什么想法我哪里出错了吗?调试器指向不相交的“int c1size = c1.size();”相关代码部分...

主要...

ClientExt1 marina = new ClientExt1("Marina",14321,"marina.calder1@btinternet.com");
ArrayList<String> interests = new ArrayList<>();
interests.add("History");
interests.add("Music");
marina.setInterests(interests);
holidaySeller.getHolidayMatches(marina);

客户端...

public class ClientExt1 {

private String name;
private int id;
private String email;
private HolidayExt1 holidayBooked;
private ArrayList<String> interests;

public ClientExt1(String name, int id, String email){
this.name=name;
this.id=id;
this.email=email;


}

public void setInterests(ArrayList interests) {
this.interests=interests;
}

public ArrayList<String> getInterests(){
return interests;

公司类别...

public class CompanyExt1 {
protected ArrayList<StaffExt1> staffMembers;
protected ArrayList<HolidayExt1> holidays;
protected ArrayList<GuideExt1> holidayGuides;
protected ArrayList<AdventureExt1> adventureHolidays;
protected ArrayList<CultureExt1> cultureHolidays;
private ArrayList<String> matchedHolidays;


public ArrayList<String> findHolidayMatch(ClientExt1 client) {
ArrayList interests = client.getInterests();
int i;
for (i = 0; i < holidays.size() && i< interests.size(); i++) {
if (!Collections.disjoint(holidays.get(i).getAspects(), interests)) {
matchedHolidays.add(holidays.get(i).getName());

}
}
return matchedHolidays;
}

public void getHolidayMatches(ClientExt1 client){
System.out.println(client.getName() + ", the holidays recommended to you based on your interests from this company are:" + findHolidayMatch(client));
}
}

最佳答案

您需要在使用之前初始化 holidays,并在 findHolidayMatch 中向其添加值之前初始化 matchedHolidays

我实际上会将matchedHolidays设置为局部变量而不是类成员,因为它仅在findHolidayMatch方法的范围内使用。

关于java - 循环数组列表并将与两个数组列表匹配的值添加到新的数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458157/

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