0

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;
}
Gaël J
  • 11,274
  • 4
  • 17
  • 32
awomtm
  • 1
  • 3
  • Maybe you can take a look at Guava's transform list, as explained here: http://stackoverflow.com/questions/7383624/how-to-transform-listx-to-another-listy – facundofarias Oct 25 '15 at 11:17

2 Answers2

2

You can use the toArray() method from your List object :

String[] phoneNosArray = new String[phoneNos.size()];
phoneNosArray = phoneNos.toArray(phoneNosArray);
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

First of all, change your ArrayList to :

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

since you store Strings in it.

Then you can get the corresponding array with :

String[] arr = phoneNos.toArray(new String[phoneNos.size()]);
Eran
  • 387,369
  • 54
  • 702
  • 768