【问题标题】:Change ArrayList from an Object to more specific type将 ArrayList 从 Object 更改为更具体的类型
【发布时间】:2018-11-22 20:54:32
【问题描述】:

是否可以更改数组列表的对象类型,即从对象 ArrayList 到特定对象 ArrayList。我试过为每个使用一个。或者,有没有一种方法可以更改文件处理方法,使其可以根据读取的文件返回特定类型,而无需重复代码?

我的尝试:

ArrayList<Object> librarianList = FileHandling.getLibrarianRecords(fileName);
ArrayList<Librarian> libList = new ArrayList<>();
for (Object addType: librarianList) {
libList.add(addType);
}

getLibrarianRecords 代码

public static ArrayList<Object> getLibrarianRecords(String filename){
                ArrayList<Object> fromFile = new ArrayList<>(); //Array of
                // existing librarians

                try{
                        FileInputStream fIS =
                                new FileInputStream(SYSTEM_PATH + filename);
                        ObjectInputStream oIS = new ObjectInputStream(fIS);
                        fromFile = (ArrayList<Object>)oIS.readObject();
                } catch (IOException ex){
                        System.out.println("Failed to read from file " + ex.getMessage());
                        ex.printStackTrace(); //Catches an IO exception.
                } catch (ClassNotFoundException ex){
                        System.out.println("Error class not found" + ex.getMessage());
                        ex.printStackTrace(); //Catches a class not found
                        // exception.
                }

                return fromFile; //Returns the array list.
        }

【问题讨论】:

  • 您的文件是什么样的?Librarian 类是什么样的?
  • 这段代码有效吗?可以显示输入输出吗?
  • 为什么不首先从您的 read 方法中返回一个图书馆员列表?
  • 代码不起作用,因为它不是正确的对象类型。我得到一个无法转换的错误。当我指定对象类型时,文件处理方法有效。 @死侍
  • 我有大约 8 个类似的方法,其中只有数组列表的对象类型发生了变化。我试图在不重新输入的情况下回收代码。 @大牛

标签: java object arraylist file-handling object-type


【解决方案1】:

从这样的文件中读取对象很少是一个好主意。也就是说,您真正需要做的就是将oIS.readObject() 的结果转换为ArrayList&lt;Librarian&gt;,而不是将其传递给ArrayList&lt;Object&gt;(就像您现在所做的那样),然后修改getLibrarianRecords 的返回类型。哦,当然还有局部变量fromFile的类型。

    public static ArrayList<Librarian> getLibrarianRecords(String filename){
            ArrayList<Librarian> fromFile = new ArrayList<>(); //Array of
            // existing librarians

            try{
                    FileInputStream fIS =
                            new FileInputStream(SYSTEM_PATH + filename);
                    ObjectInputStream oIS = new ObjectInputStream(fIS);
                    fromFile = (ArrayList<Librarian>)oIS.readObject();
            } catch (IOException ex){
                    System.out.println("Failed to read from file " + ex.getMessage());
                    ex.printStackTrace(); //Catches an IO exception.
            } catch (ClassNotFoundException ex){
                    System.out.println("Error class not found" + ex.getMessage());
                    ex.printStackTrace(); //Catches a class not found
                    // exception.
            }

            return fromFile; //Returns the array list.
    }

然后应该不需要遍历列表来实际逐个元素地进行类型转换。

【讨论】:

    【解决方案2】:

    感谢@user3170251 建议选角

    ArrayList<Object> librarianList = FileHandling.getLibrarianRecords(fileName);
    ArrayList<Librarian> libList = new ArrayList<>();
    for (Object addType: librarianList) {
    libList.add((Librarian) addType);
    }
    

    对于更改类型,这确实有效。

    【讨论】:

      【解决方案3】:

      你可以使用这个泛型类定义

      class ArrayListConverter<T> {
        List<T> cast;
        public ArrayListConverter(List<T> typeList){
         cast = new ArrayList<>();
         for (Object addType: typeList) {
            cast.add((T)addType);
         }
      
        }
        public List<T> getList(){
           return cast;
        }
      
      }
      

      这样做:

      ArrayListConverter<Librarian> conv = new ArrayListConverter<>(libList);
      ArrayList<Librarian> libListOg  = conv.getList();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-23
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        • 2017-10-09
        • 2016-11-19
        相关资源
        最近更新 更多