如何在java中搜索类类型的arraylist

时间:2016-02-11 11:22:03

标签: java search arraylist

我正在尝试创建一个方法searchMachine(),它接受一个参数并打印出我用该参数找到的机器号(数组列表中的索引号)(在本例中是成本)。这是我的代码由于某种原因,它跳过了即使添加了具有该成本的机器也总是找不到的条件。我也在场上的TicketMachine周围有尖括号,网站不会显示它。我的第一篇文章非常适合编程。欢迎任何建议。

public class AutomatedTicketOffice
{
  private ArrayList <TicketMachine>  newMachine;

public AutomatedTicketOffice()
{
  newMachine = new ArrayList<TicketMachine>();
}

public void addTicketMachine(int cost)
 {
  TicketMachine machine = new TicketMachine(cost);
  newMachine.add(machine);
  }

public void insertMoney(int machineNumber, int amount)
  { 
   TicketMachine machine = newMachine.get(machineNumber);
   machine.insertMoney(amount);

  }

public void printTicket()
{  
  for (TicketMachine machine : newMachine)
  {
      machine.printTicket();
  }
}

public void listTicketMachines()
{  int counter = 1;
  int noOfTicketMachines= newMachine.size();  
  if (counter < noOfTicketMachines)
   {
    for(TicketMachine machine : newMachine)
    {System.out.println("Machine no " + ": "+ counter);    

     counter++;
    }
  } 
}

public void searchMachine(int cost)
{ int machineIndex= 0;
  boolean found = false;
  while ( machineIndex < newMachine.size() && !found){

      if (newMachine.contains(cost))
      {System.out.println("Machine no " + " : " + machineIndex );   

         found = true;      
    } else {
      machineIndex++;
      System.out.println("There is no such Ticket Machine");
   } 

  }
}

}

3 个答案:

答案 0 :(得分:1)

您必须遍历整个ArrayList并手动比较TicketMachine

for (int i = 0; i < newMachine.size(); i++) { 
      // newMachine is a terrible name for a list, just saying
      if (newMachine.get(i).getCost() == cost) {
         System.out.println("Machine no: " + i);
         found = true;
         // break; // depends if you just want to find one or all of them
      }
}
if (!found)
    System.out.println("There is no such Ticket Machine");

当然TicketMachine必须允许访问其参数。

public class TicketMachine {
       private int cost;
       // other variables

       // constructor, i.e.,
       public TicketMachine(int cost) {
          this.cost = cost;
       }

       public int getCost() {
          return cost;
       }
}

如果costpublic字段,您可以通过newMachine.get(i).cost循环访问该字段,而不是使用Getter访问权限。

编辑:因为你问过。

for (TicketMachine machine : newMachine) {
    if (machine.getCost() == cost) {
        System.out.println("Machine no: " + newMachine.indexOf(machine));
        found = true;
        // break; // depends if you just want to find one or all of them
    }
}

与上面的循环相同。

答案 1 :(得分:1)

public void searchMachine(int cost) {
    for (int i = 0; i < newMachine.size(); i++) { 
        if (newMachine.get(i).getCost() == cost) {
            System.out.println("Machine number is: " + i);  
        }
    }
}

如果要返回位置编号,可以执行以下操作:

public int searchMachine(int cost) {
    for (int i = 0; i < newMachine.size(); i++) { 
        if (newMachine.get(i).getCost() == cost) {
            System.out.println("Machine number is: " + i);  
            return i;
        }
    }
    return 0;
}

如果要返回布尔值(truefalse),可以执行以下操作:

public boolean searchMachine(int cost) {
    for (int i = 0; i < newMachine.size(); i++) { 
        if (newMachine.get(i).getCost() == cost) {
            System.out.println("Machine number is: " + i);  
            return true;
        }
    }
    return false;
}

答案 2 :(得分:0)

感谢帮助伙伴,我用一个while循环做了它,我缺少的主要观点是ArrayList的get方法,并使用了TicketMachine类的getPrice方法。这个编译并运行其他建议。< / p>

public void searchMachine(int cost)
{ int index= 0;
  boolean found = false; 
   while ( index < TicketMachine.size()&& !found){

     if (TicketMachine.get(index).getPrice() == cost)
     {System.out.println("Machine no " + " : " + index );   

         found = true;      
    } else 
       {System.out.println("There is no such Ticket Machine");
            index++;
       }
  }

}