Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Can anyone tell me if I'm using the proper code to print the occurrences in this ArrayList?

Okay, so what I'm trying to do is go through the ArrayList called theDateArray, do a search for a particular item in that list, and output how many times that item appears. When I test it in the main class, it just outputs the number 0 no matter what date as a String I enter in. Here is part of the class which the methods are in. I don't think as of now it's necessary to include the entire class unless it is confusing...

  public ArrayList<String> getTicketDates(){

           int i;

           for (i=0; i <tickets.size(); i++){
               if(tickets .get(i).getPurchased()== false){
                 theDateArray.add(tickets.get(i).getDate());
               }
             }
             for(int f=0; f<theDateArray.size();f++){
               System.out.println(theDateArray.get(f)+ " ");
             }
             return theDateArray;
           }       



     public int getTickets(String date){
         int tix= theDateArray.indexOf(date);
         int occurrences= Collections.frequency(theDateArray, tix);
         if (tix>=0){
             System.out.println(occurrences);


         }
         return occurrences;
     }

Here is the made up tickets that I'm testing in my test class....

 public class AmusementParkTester {

public static void main(String[] args) {


    AmusementPark park1= new AmusementPark("Walden University Park");

    park1.addTicket(777, "Child", "Noah Johnson","2017-06-09" , 27.99, false);
    park1.addTicket(777, "Child", "Zachary Gibson","2017-06-09" , 27.99, false);
    System.out.println("The dates in which tickets are available are as follows: ");
    park1.getTicketDates();

            park1.getTickets("2017-06-09");

    }



}

Here is the output I'm getting....

    The dates in which tickets are available are as follows: 
    2017-06-09 
    2017-06-09 
    0

I want the 0 to be a 2.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think these lines are your error:

int tix= theDateArray.indexOf(date);
int occurrences= Collections.frequency(theDateArray, tix);

You're looking for the frequency of the index of your date, so in your example the first index of 2017-06-09 date might be index 0? So when you do Collections.frequency ... you're looking for the frequency of 0 within your date array, to which it returns 0 (no occurences).

I would try switching it over to

// find the number of occurrences of your passed in date within the date array
int occurrences = Collections.frequency(theDateArray, date)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!