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

209
Views
¿Alguien puede decirme si estoy usando el código adecuado para imprimir las ocurrencias en este ArrayList?

De acuerdo, entonces lo que estoy tratando de hacer es revisar el ArrayList llamado theDateArray, hacer una búsqueda de un elemento en particular en esa lista y mostrar cuántas veces aparece ese elemento. Cuando lo pruebo en la clase principal, solo genera el número 0 sin importar la fecha que ingrese como una Cadena. Aquí hay parte de la clase en la que están los métodos. No creo que a partir de ahora sea necesario incluir toda la clase a menos que sea confuso...

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

Aquí están los boletos inventados que estoy probando en mi clase de prueba....

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

Aquí está la salida que estoy obteniendo ...

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

Quiero que el 0 sea un 2.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Creo que estas líneas son tu error:

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

Está buscando la frecuencia del índice de su fecha, por lo que en su ejemplo, el primer índice de la fecha 2017-06-09 podría ser el índice 0. Entonces, cuando haces Collections.frequency ... estás buscando la frecuencia de 0 dentro de tu matriz de fechas, a la que devuelve 0 (sin ocurrencias).

Yo intentaría cambiarlo a

 // 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!