【问题标题】:Java method for checking whether an element of an array exists用于检查数组元素是否存在的Java方法
【发布时间】:2021-10-26 17:58:12
【问题描述】:

我有两个类,分别称为 Ticket 和 Register。 Register 是一个接收票证数组的类。工单包含一个时间和一个带有工单 ID 的字符串。我的注册类中的一种方法称为 contains(),它返回一个布尔值。它检查寄存器是否包含具有该 ID 的票证。我的问题是它只对第一个票元素显示为真,而对所有其他票元素显示为假,即使它包含票,因此我相信问题出在我的 contains() 函数上。我对Java非常了解,所以如果有人可以帮助我,将不胜感激。这是我的注册代码和我的驱动程序代码。

/
// This is class called Register that has the methods:"add()","contains()","retrieve()" and the constructor "Register()"

public class Register{ 

public Ticket[] tickets; 
public int numTickets; 

public Register(){ 
tickets = new Ticket[100]; 
numTickets = 0; 

}

public void add(Ticket ticket){ //add ticket method. Takes a ticket as a parameter and adds it to the array

tickets[numTickets] = ticket;
numTickets = numTickets+1; // incramenting array

}


public boolean contains(String ticketID){ //contains method. Takes a string from the ticket using the ID() method and uses as a parameter.

boolean b = false; 

for (int i = 0;i<100;i++){ 

if (ticketID.equals(tickets[i].ID())){ // checks to see if ticket ID is in the array
b = true; // return value is true if found
break;
}
break; 
}
return b; // returns true of false
}

public Ticket retrieve(String ticketID){ // returns the ticket with the specified ID

int j = 0; // initializing return value
for (int i = 0;i<100;i++){ 

if (ticketID.equals(tickets[i].ID())){ 

j = i; 
break;     
}

}

return tickets[j]; // returns ticket
}


}

这是我的驱动程序代码

public static void main(String[] args){


Register r = new Register();
Ticket t;
t = new Ticket(new Time("13:00"), "00001");
String ID_One = t.ID();
r.add(t);

t = new Ticket(new Time("13:18"), "00002");
String ID_Two = t.ID();
r.add(t);
System.out.println(r.contains(ID_One));
System.out.println(r.contains("00002"));
System.out.println(r.retrieve(ID_Two).toString());

输出是:

true
false // this should be true
Ticket[id=00002, time=13:18:00] 

【问题讨论】:

  • 首先,您应该正确缩进您的代码。缩进可以帮助你自己和其他人阅读你的代码。其次,您有两个 break 语句。第一个可能属于那里,第二个是无条件执行的,所以循环总是在第一次迭代后退出。

标签: java function class for-loop oop


【解决方案1】:

您在if 条件之外使用break 语句,因此它只会检查第一个元素然后中断循环。您可以通过更好的格式立即看到这一点。

for (int i = 0; i<100; i++){ 
  if (ticketID.equals(tickets[i].ID())){ // checks to see if ticket ID is in the array
    b = true; // return value is true if found
    break;
  }
  break; 
}

删除第二个break 以解决此问题,这样您就只会在if 条件为真时中断。

对于个人建议,如果您对每张票都有唯一的 ID,您可能需要考虑使用 Map 结构。这可以大大减少您的搜索时间。

int numTickets = 0;
Map<String, Ticket> ticketMap = new HashMap<>();

public void add(Ticket ticket) {
  numTickets++;
  ticketMap.put(ticket.ID(), ticket);
}

public boolean contains(String ticketId) {
  return ticketMap.containsKey(ticketId);
}

public Ticket retrieve(String ticketId) {
  return ticketMap.get(ticketId);
}

【讨论】:

    【解决方案2】:

    删除第二个break,它在第一次迭代时总是退出for 循环,因此只比较数组的第一个元素。

    您可以将代码简化为 1 行:

    public boolean contains(String ticketID) {
        return Arrays.stream(tickets).map(Ticket::ID).anyMatch(ticketID::equals);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多