2013-03-23 162 views
0

如何使用BeanUtils.populate方法將String []轉換爲ArrayList?它看起來像我可以使用ArrayConverter類,但我很難找到解釋如何做到這一點。如何使用BeanUtils.populate方法將String []轉換爲ArrayList <String>?

下面是一些測試代碼,我創建:

public class TestAction { 
public static TestBean testPopulate() throws IllegalAccessException, InvocationTargetException { 
    HashMap<String, Object> map = new HashMap<String, Object>(); 
    TestBean bean = new TestBean(); 
    String[] names = {"teststring","testboolean","testinteger","testarray"}; 
    String[] array = {"hi","bye"}; 
    Object[] values = {"TEST","","100",array}; 
    int i = 0; 
    while (i < names.length) { 
     String name = names[i]; 
     map.put(name, values[i]); 
     i++; 
    } 
    BeanUtils.populate(bean, map); 

    return bean; 
} 

public static void main(String[] args) { 
    try { 
     TestBean bean = testPopulate(); 
     System.out.println(bean.getTeststring() + " " + bean.getTestinteger() + " " + bean.getTeststring() + " " + bean.isTestboolean()); 
    } catch (IllegalAccessException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 

public class TestBean { 
private String teststring; 
private boolean testboolean; 
private int testinteger; 
private ArrayList<String> testarray; 

public String getTeststring() { 
    return teststring; 
} 
public void setTeststring(String teststring) { 
    this.teststring = teststring; 
} 
public boolean isTestboolean() { 
    return testboolean; 
} 
public void setTestboolean(boolean testboolean) { 
    this.testboolean = testboolean; 
} 
public int getTestinteger() { 
    return testinteger; 
} 
public void setTestinteger(int testinteger) { 
    this.testinteger = testinteger; 
} 
public ArrayList<String> getTestarray() { 
    return testarray; 
} 
public void setTestarray(ArrayList<String> testarray) { 
    this.testarray = testarray; 
} 

} 

我傳遞一個字符串[]類型期待它知道如何轉換爲ArrayList中,但它失敗。

回答

2

這是因爲我不通用的答案通常與豆一起工作。這可能是BeanUtils 應該處理這種情況,但它會讓我感到驚訝。

字符串數組與ArrayList對象不同,儘管它們有相似之處。隱含地從一個轉換到另一個通常是一個糟糕的主意,因爲有多種邏輯方法可以完成。

  • 可以使用Arrays.asList()它接受一個數組並返回一個包裝List對象。請注意,它不是ArrayList,而是一個固定大小的自定義實現List,它直接與傳遞的數組進行接口,這意味着不需要分配新存儲。

  • 相反,如果你真的想要一個ArrayList,您可以構建一個基於關你得到了List對象,有一個新問題:

    ArrayList<String> ls = new ArrayList<String>(Arrays.asList(myArray)); 
    

    這個調用分配一個新的存儲陣列,但如果你想調整大小,你必須接受O(n)的成本。

這兩個可能的選擇,這兩者都取決於你的使用情況同樣有效的,是完全爲什麼它會是一個糟糕的主意,你的bean隱式轉換爲數組List - 它應該選擇哪種實現?如果您嘗試調整大小,會拋出異常,或者速度較慢的那個速度較慢?

只要有可能,明確您的類型轉換。儘管冗長看似/令人討厭,但這是Java最強大的特性之一。

+0

好吧,我現在明白了。我將String []更改爲ArrayList ,並且代碼沒有錯誤地運行。從String []轉換爲ArrayList 時,BeanUtils故意失敗。作爲Java中的HttpServletRequest類返回String或String []類型的後續問題,那麼是否強制我在我的bean中僅使用基本類型和String []類型? – user2191540 2013-03-23 22:42:00

+0

「HttpServletRequest」的哪個方法?它並不強迫你,但是,如果在bean中傳遞的數據類型是一個String [],那麼在你的bean中使用數組可能是有意義的,然後當它們轉換爲List對象時你需要。或者,做一些預處理。在將映射傳遞給BeanUtils.populate之前,請構建要使用的'List'對象,並將其放入映射中。 – dimo414 2013-03-23 22:54:59

+0

啊...預處理是一個好主意,我會嘗試一下。感謝您的建議。 – user2191540 2013-03-23 23:42:52

1

你沒有提及究竟失敗在你的代碼,但在這裏是如何轉換字符串數組沒有任何第三方實用程序列表:

Arrays.asList(new String[] {"aaa", "bbb", "ccc"})

相關問題