Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda