private ShortAndLowFlyTradeNode buildTree(List<FlightDateAndAirport> stopFlight, PassengerParam passengerParam, ShortAndLowFlyTradeNode root, Set<String> usedStopFlight) { for (int i = 1; i < stopFlight.size(); i++) { ShortAndLowFlyTradeNode temp = new ShortAndLowFlyTradeNode(2, passengerInfoMapper.getPassengerInfo(passengerParam, stopFlight.get(i)), null); if (root.getShortTrade().getArriveAirThreeCode().equals(stopFlight.get(i).getDepartAirThreeCode()) && stopFlight.get(i).getDepartTime().after(root.getShortTrade().getETA())) { //子节点 System.out.println(stopFlight.get(i).getFlightNumber() + "是" + root.getShortTrade().getFlightNumber() + "的后续航程"); //前端要空数组,所以给个初始化 temp.setNode(new ArrayList<>()); root.getNode().add(temp); //添加进已使用的中转航程集合中 usedStopFlight.add(temp.getShortTrade().getFlightNumber()); } else { //叶子节点 root = addChild(root, temp, usedStopFlight); } } return root; }
时间: 2024-04-10 14:30:44 浏览: 12
这段代码是一个构建树的方法。它接收一个包含航班日期和机场信息的列表 `stopFlight`,乘客参数 `passengerParam`,根节点 `root` 和已使用的中转航班集合 `usedStopFlight`。方法通过遍历 `stopFlight` 列表来构建树。
首先,通过索引从1开始遍历 `stopFlight` 列表。对于每个索引 `i`,创建一个临时节点 `temp`,并将乘客信息传递给 `passengerInfoMapper.getPassengerInfo` 方法来获取乘客信息。
然后,检查临时节点的到达机场三字码是否与根节点的出发机场三字码相同,并且临时节点的出发时间是否在根节点的预计到达时间之后。如果满足这些条件,将临时节点添加为根节点的子节点,并将其添加到根节点的节点列表中,同时将临时节点的航班号添加到已使用的中转航班集合中。
如果不满足条件,则将临时节点添加为根节点的子节点,并递归调用 `addChild` 方法来添加子节点的子节点。
最后,返回根节点。
请注意,这段代码中使用了一些未定义的方法和类,例如 `ShortAndLowFlyTradeNode`、`FlightDateAndAirport` 和 `PassengerParam`。你可能需要提供这些定义才能更好地理解代码。
相关问题
private Dictionary<int, List<List<string>>> list = new Dictionary<int, List<List<string>>>();是什么意思
这是一个定义了一个泛型字典(Dictionary)的变量,键类型为 int,值类型为 List<List<string>>。其中,List<List<string>> 是一个包含多个 List<string> 的列表,而 List<string> 则是一个包含多个字符串的列表。整个变量的含义是,存储了一个键为整数,值为多个字符串列表的字典。
public class Game { private static final String[] suits = {"♥","♣","♦","♠"}; public List buyPoker() { List<Poker> pokers = new ArrayList<>(); for (int i = 0; i < 4; i++) { for (int j = 0; j <= 13; j++) { Poker poker = new Poker(suits[i],j); pokers.add(poker); } } return pokers; } public void shuffle(List<Poker> pokers) { for (int i = pokers.size()-1; i > 0; i--) { Random random = new Random(); int index = random.nextInt(i); swap(pokers,i,index); } } private void swap(List<Poker> pokers,int i,int j) { Poker tmp = pokers.get(i); pokers.set(i,pokers.get(j)); pokers.set(j,tmp); } public List<List<Poker>> game(List<Poker> pokers) { List<List<Poker>> hand = new ArrayList<>(); List<Poker> hand1 = new ArrayList<>(); List<Poker> hand2 = new ArrayList<>(); List<Poker> hand3 = new ArrayList<>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Poker removePoker = pokers.remove(0); hand.get(j).add(removePoker); } } return hand; } }有错吗
在game方法中,你创建了一个空的List<List<Poker>> hand,但没有将任何元素添加到它中。因此,在尝试将元素添加到hand.get(j)时,会抛出NullPointerException异常。为了解决这个问题,你需要在game方法中为hand添加3个空列表,然后才能向它们中添加元素。
你需要将game方法中的代码修改如下:
```
public List<List<Poker>> game(List<Poker> pokers) {
List<List<Poker>> hand = new ArrayList<>();
List<Poker> hand1 = new ArrayList<>();
List<Poker> hand2 = new ArrayList<>();
List<Poker> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Poker removePoker = pokers.remove(0);
hand.get(j).add(removePoker);
}
}
return hand;
}
```
这样就能成功地将元素添加到hand中了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)