[英]Create array of string from an Arraylist
I want to get : 我想得到:
String[] phoneNos= {"07064267499","07064267499","07064267499"};
from : 来自:
ArrayList<Object> phoneNos = [0803406914, 08167337499, 0803377973] ;
This is my code snippet : 这是我的代码片段:
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;
}
You can use the toArray()
method from your List
object : 您可以从
List
对象使用toArray()
方法:
String[] phoneNosArray = new String[phoneNos.size()];
phoneNosArray = phoneNos.toArray(phoneNosArray);
First of all, change your ArrayList
to : 首先,将
ArrayList
更改为:
ArrayList<String> phoneNos = new ArrayList<String>();
since you store String
s in it. 因为您在其中存储
String
。
Then you can get the corresponding array with : 然后,您可以使用以下命令获取相应的数组:
String[] arr = phoneNos.toArray(new String[phoneNos.size()]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.