以下是ticket类的定义:package search; import java.util.Date; public class ticket { private int id; private String number; private String wherestart; private String gotowhere; private Date start_time; private Date end_time; private int counts; private double price; private Date order_time; public void setId(int id) { this.id=id; } public int getId() { return id; } public void setNumber(String number) { this.number=number; } public String getNumber() { return number; } public void setWherestart(String wherestart) { this.wherestart=wherestart; } public String getWherestart() { return wherestart; } public void setGotowhere(String gotowhere) { this.gotowhere=gotowhere; } public String getGotowhere() { return gotowhere; } public void setStart_time(Date start_time) { this.start_time=start_time; } public Date getStart_time() { return start_time; } public void setEnd_time(Date end_time) { this.end_time=end_time; } public Date getEnd_time() { return end_time; } public void setAirEnterPrice(double price) { this.price=price; } public double getAirEnterPrice() { return price; } public void setCounts(int counts) { this.counts=counts; } public int getCounts() { return counts; } public void setOrder_time(Date order_time) { this.order_time=order_time; } public Date getOrder_time() { return order_time; } },如何把上述的string 数组selectedTickets中的数据存入ticket数组tickets
时间: 2024-02-19 11:57:28 浏览: 112
首先,我们需要将`ticket`类的定义放在与`Servlet`同一个包下,然后修改`ticket`类的定义,为`ticket`类添加一个`selected`属性,用于标记该机票是否被用户选择。修改后的`ticket`类定义如下:
```
package search;
import java.util.Date;
public class ticket {
private int id;
private String number;
private String wherestart;
private String gotowhere;
private Date start_time;
private Date end_time;
private int counts;
private double price;
private Date order_time;
private boolean selected; // 新增属性
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setWherestart(String wherestart) {
this.wherestart = wherestart;
}
public String getWherestart() {
return wherestart;
}
public void setGotowhere(String gotowhere) {
this.gotowhere = gotowhere;
}
public String getGotowhere() {
return gotowhere;
}
public void setStart_time(Date start_time) {
this.start_time = start_time;
}
public Date getStart_time() {
return start_time;
}
public void setEnd_time(Date end_time) {
this.end_time = end_time;
}
public Date getEnd_time() {
return end_time;
}
public void setAirEnterPrice(double price) {
this.price = price;
}
public double getAirEnterPrice() {
return price;
}
public void setCounts(int counts) {
this.counts = counts;
}
public int getCounts() {
return counts;
}
public void setOrder_time(Date order_time) {
this.order_time = order_time;
}
public Date getOrder_time() {
return order_time;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public boolean isSelected() {
return selected;
}
}
```
接下来,在`Servlet`中定义一个`List`类型的变量`tickets`,用于存储用户选择的机票对象。然后通过以下代码来将`selectedTickets`数组中的数据存入`tickets`数组中:
```
List<ticket> tickets = new ArrayList<>();
if (selectedTickets != null) {
for (ticket t : ticketList) {
for (String selectedTicket : selectedTickets) {
if (String.valueOf(t.getId()).equals(selectedTicket)) {
t.setSelected(true); // 将用户选择的机票标记为已选择
tickets.add(t); // 将已选择的机票添加到tickets数组中
}
}
}
}
```
以上代码首先对`tickets`进行了初始化,然后对`ticketList`(一个包含所有机票对象的列表)进行`for`循环遍历,对于每个机票对象,再对`selectedTickets`数组进行`for`循环遍历,判断该机票对象是否被用户选择,如果是,就将该机票对象的`selected`属性设置为`true`,并将该机票对象添加到`tickets`数组中。这样,`tickets`数组就存储了用户选择的机票对象。
阅读全文