Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda