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

267
Views
¿Cómo accedo a una ArrayList desde un método anterior en la misma clase?

En este proyecto, intento acceder a la información de una ArrayList que contiene solo las fechas que son cadenas.

Aquí está parte de la clase que probé. Si no tener toda la clase hace que sea difícil de entender, puedo editar...

 public ArrayList<String> getTicketDates(){ ArrayList<String> theDateArray= new ArrayList<>(); 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; }

La segunda clase, estoy tratando de contar la cantidad de veces que ocurre una fecha en particular en el ArrayList anterior, pero dice que DateArray no se puede resolver en una variable.

Un método que probé es simplemente llamar al método completo getTicketDates(), pero lo que hace es imprimir el triple de ArrayList y las ocurrencias aún no funcionan.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El ámbito de la variable theDateArray es local para el método getTicketDates() , por lo que no puede acceder a él en el otro método, así que declárelo como una variable de instancia como se muestra a continuación:

 public class YourTicketsClass { //declare ArrayList as an instance variable ArrayList<String> theDateArray= new ArrayList<>(); 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; } }
over 4 years ago · Santiago Trujillo Report

0

Defina la lista de arreglos fuera del método, luego complete la lista dentro del método. Me gusta esto:

 public class YourdataClass { private List<String> theDateArray = new ArrayList<String>(); 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; } }/* end class */

Aquí hay algunas referencias. http://www.javawithus.com/tutorial/scope-and-lifetime-of-variables https://en.wikibooks.org/wiki/Java_Programming/Scope

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!