从Arraylist创建字符串数组

时间:2015-10-25 11:15:18

标签: java mysql

我想得到:

String[] phoneNos= {"07064267499","07064267499","07064267499"};

来自:

ArrayList<Object> phoneNos = [0803406914, 08167337499, 0803377973] ;

这是我的代码段:

public ArrayList < Object > getPhoneNo() {
    ArrayList < Object > phoneNos = new ArrayList < Object > ();
    try {
        Statement stmt = con.createStatement();
        ResultSet result = stmt.executeQuery("SELECT Phone_No FROM paient_details");
        while (result.next()) {
            phoneNos.add(result.getString(1));
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }

    return phoneNos;
}

2 个答案:

答案 0 :(得分:2)

您可以使用toArray()对象中的List方法:

String[] phoneNosArray = new String[phoneNos.size()];
phoneNosArray = phoneNos.toArray(phoneNosArray);

答案 1 :(得分:1)

首先,将您的ArrayList更改为:

ArrayList<String> phoneNos = new ArrayList<String>();

因为您将String存储在其中。

然后你可以得到相应的数组:

String[] arr = phoneNos.toArray(new String[phoneNos.size()]);
相关问题