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