【问题标题】:How do I access an ArrayList from a previous method in the same class?如何从同一类中的先前方法访问 ArrayList?
【发布时间】:2017-09-26 18:57:01
【问题描述】:

在这个项目中,我试图从仅包含字符串日期的 ArrayList 中访问信息。

这是我尝试过的课程的一部分。如果没有全班让我很难理解我可以编辑......

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

第二类,我试图计算一个特定日期在前一个 ArrayList 中出现的次数,但它说 theDateArray 无法解析为变量。

我尝试过的一种方法是调用整个方法 getTicketDates(),但它的作用是打印出 ArrayList 三元组,但出现的情况仍然不起作用。

【问题讨论】:

  • 你应该阅读java中的变量范围。您的 arraylist 当前是一个局部变量,因此仅在局部范围内可用。
  • 对不起,我还是个新手。

标签: java arraylist methods


【解决方案1】:

您的theDateArray 变量范围是getTicketDates() 方法的本地变量,因此您无法在其他方法中访问它,因此将其声明为实例变量,如下所示:

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

【讨论】:

    【解决方案2】:

    在方法外定义数组列表,然后在方法内填充列表。像这样:

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

    这里有一些参考资料。 http://www.javawithus.com/tutorial/scope-and-lifetime-of-variables https://en.wikibooks.org/wiki/Java_Programming/Scope

    【讨论】:

    • 这与我 9 分钟前发布的答案有何不同?
    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2013-04-06
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多