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

261
Views
How do I access an ArrayList from a previous method in the same class?

In this project, I'm trying to access information from an ArrayList which contains only the dates which are Strings.

Here is part of the class I tried. If not having the whole class makes it hard to understand I can edit...

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

The 2nd class, I'm trying to count the amount of times one particular date occurs in the previous ArrayList, but it says that theDateArray cannot be resolved to a variable.

One method I've tried is just calling the entire method getTicketDates(), but what it does is it prints out the ArrayList triple, and the occurrences still don't work.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your theDateArray variable scope is local to the getTicketDates() method, so you are not able to access it in the other method, so declare it as an instance variable as shown below:

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

Define the array list outside of the method, then populate the list inside the method. Like this:

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

Here are some references. 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!