【问题标题】:My ArrayList Not Returning all the elements我的 ArrayList 没有返回所有元素
【发布时间】:2018-12-09 04:42:34
【问题描述】:

当我使用 System.out.println 静态方法时,下面的我的 Java 程序会显示 ArrayList 中的所有元素。但是,当我在方法中返回列表时,它只显示 ArrayList 中的一个元素。我会感谢一些关于做错的方向:

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileProcessor {  
  static List<String> theList = null;

  /**
   * 
   * @return List
   */
  public static List<String> processFiles() {      
    try {    
      File f = new File("/Data/fileDump");
      String[] listOfFiles = f.list();

      for(String eachFile: listOfFiles) {  
        if(eachFile.startsWith("hawk") == true) { 
          theList = new ArrayList<>(); 
          theList.add(eachFile); 
          return theList;
        }
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
    return theList;
  }


  public static void main(String[]args) {
    List<String> dataList = FileProcessor.processFiles(); 
    for(String strg: dataList) {
      if(strg != null) {
        System.out.println(strg);
      }
    }
  }
}

【问题讨论】:

  • 欢迎来到 Stackoverflow。请仔细阅读"how to ask a good question",然后返回您的帖子,以便您对其进行一些编辑:代码到处都是,这是一种很好的形式,可以确保不仅有足够的细节,而且还提供格式良好的代码。跨度>
  • 在循环内创建一个列表,向其中添加一个元素,然后返回它。你能明白为什么它是一个单元素列表吗?

标签: java arraylist


【解决方案1】:

将您的函数替换为以下内容。

      public static List<String>  processFiles() { 
          List<String> theList = null;
          try {    

             File  f = new File("/Data/fileDump");
             String[] listOfFiles = f.list();
             theList = new ArrayList<>(); // initialisation moved outside of loop
             for(String eachFile:   listOfFiles) {  
                 if(eachFile.startsWith("hawk") == true){              
                    theList.add(eachFile); 
               }
             }
             return theList;// return statement moved outside of the loop

          } catch(Exception e) {
             e.printStackTrace();
          }
        return theList;
       }

【讨论】:

    【解决方案2】:

    你必须在你的 for 块之外返回。否则,您将返回一个元素。您还在每个循环中重新实例化列表。

    【讨论】:

      【解决方案3】:

      试试这个代码。只有几个变化。我在我移动或删除代码的地方添加了备注。

      import java.io.File;
      import java.util.ArrayList;
      import java.util.List;
      
      public class FileProcessor {  
      static  List<String> theList = null;
      
      
      
      
      
       /**
        * 
        * @return List
        */
       public static List<String>  processFiles() {      
      
            try {    
      
      
                File  f = new File("/Data/fileDump");
      
                String[] listOfFiles = f.list();
                theList = new ArrayList<>();  /* Move this here */
                for(String eachFile:   listOfFiles) {  
                   if(eachFile.startsWith("hawk") == true){
                      theList.add(eachFile);
      
                     /* Deleted the extra return. The one at the end will handle it. */
      
                   }
                }
      
            } catch(Exception e) {
      
      
               e.printStackTrace();
            }
          return theList;
         }
      
      
          public static void main(String[]args){
           List<String> dataList = FileProcessor.processFiles(); 
           for(String strg: dataList){
               if(strg != null){
                  System.out.println(strg);
               }
           }
      
      
          }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-13
        • 2021-01-19
        • 1970-01-01
        • 2015-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多