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;
}
1

2 Answers 2

2

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

String[] phoneNosArray = new String[phoneNos.size()];
phoneNosArray = phoneNos.toArray(phoneNosArray);
Sign up to request clarification or add additional context in comments.

Comments

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()]);

5 Comments

@awomtm What do you mean by that? What null was returned? Where?
return arr; gives null. I am a java newbie anyway.
@awomtm Can you show the actual code giving the error?
Add comment says my code is too long. Pls is there any other way to repost my code
@awomtm You can edit your question and post it there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.