简体   繁体   中英

How can i search an arraylist of a class type in java

I'm trying to make a method searchMachine() which takes a parameter and prints my machine number(the index number in the Array List) that it finds with that parameter(in this case the cost). Here is my code for some reason , it skips the condition always going to not found even when a machine with that cost has been added. Also ive got angle brackets around the TicketMachine in the field , the website wont show it. My first post pretty new to programming.Any advice would be welcome.

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");
   } 

  }
}

}

You have to iterate through the whole ArrayList and compare the TicketMachine s manually.

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");

Of course TicketMachine must allow the access to its parameters.

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

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

       public int getCost() {
          return cost;
       }
}

If cost is a public field, you can access it in the loop via newMachine.get(i).cost instead of using the Getter access.

Edit: because you asked.

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
    }
}

does the same as the loop above.

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);  
        }
    }
}

If you want to return the position number, you can do this:

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;
}

If you want to return the boolean ( true or false ), you can do this:

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;
}

Thanks for the help guys i did it with a while loop the main point i was missing was the get method of the ArrayList and using one the getPrice method of the TicketMachine class.This compiles and runs so do the others suggested.

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++;
       }
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM