我写了一些代码来返回一个对象数组。如何在被调用的方法中保存这些对象?
public Ticket[] getOpenTicket() {
int ticketcount = 0;
for (int i = 0; i < ticket.length; i++) {
if (ticket[i] != null && ticket[i].getResolvedBy() == null) {
ticketcount = ticketcount + 1;
// System.out.println("Ticket raised by : " +
// ticket[i].getTicketno());
}
}
Ticket[] opentickets = new Ticket[ticketcount];
for (int i = 0; i < ticket.length; i++) {
if (ticket[i].getResolvedBy() == null) {
opentickets[i] = ticket[i];
}
}
return opentickets;
}
这是我调用openticket的被调用函数:
TicketDaoMemImpl tdmi=new TicketDaoMemImpl();
Ticket [] obj1=tdmi.getOpenTicket();
谢谢
答案 0 :(得分:1)
不应该看起来更像这样:
public class CheckTicket {
public Ticket [] openTicket() {
return arrayOfTickets; // wherever that comes from
}
}
CheckTicket cc = new CheckTicket();
Ticket[] t1 = cc.openTicket();
答案 1 :(得分:0)
在这行代码中
Ticket[] opentickets = new Ticket[ticketcount];
for (int i = 0; i < ticket.length; i++) {
if (ticket[i].getResolvedBy() == null) {
票证[i]不能为空吗?似乎这很可能导致您的问题 - 您可以在可能为空引用的地方调用方法。
您应该将循环更改为:
Ticket[] opentickets = new Ticket[ticketcount];
int ticketIndex = 0;
for (int i = 0; i < ticket.length; i++) {
if (ticket[i] != null && ticket[i].getResolvedBy() == null) {
opentickets[ticketIndex] = ticket[i];
ticketIndex++;
}
}