12345
12345

Reputation: 143

how to convert arraylist to string array with all items in double quote

In my application i m getting a problem.. i am getting data from server into ArrayList. but i am unable to convert that list in to a String array in which all data items are in double quoted.

my code for getting arrayList is

public ArrayList<String> dataarea1(){   

        ArrayList<String>dataarea1=new ArrayList<String>();
        try{
        Statement smt=mycon.connection().createStatement();
        rs=smt.executeQuery("DCRAREADDL "+ paid +",'','','',''");

        while(rs.next())
        {
            dataarea1.add(rs.getString("AREA"));
        }
        }catch(Exception e){
            e.printStackTrace();
        }
        return dataarea1;
    }

now i want to convert my ArrayList dataarea1 to a StringArray like this..

String[] colours = { "Red", "Green", "Blue", "Yellow", "Orange", "Purple" };

i want to convert my arraylist in this form but now it is converting in single quoted only.... if some one can tell me how to convert arraylist in Stringarray with all data item in double qoute

now i m using this code fro getting string array

public String GetName()
     {
         String colour = "";
            for (int i = 0; i < dataarea3.size(); i++) {
                colour += ""+ dataarea3.get(i).toString() +"'"+ ",";

            }
            return colour;
     }   

but here all items are placed in single quote........how i can put them in double quote

Upvotes: 3

Views: 10540

Answers (6)

Abhishek Birdawade
Abhishek Birdawade

Reputation: 301

Here is the modified function.You can use backslash("\") to put quotes.

public String GetName() {

     String colour = "";

        for (int i = 0; i < dataarea3.size(); i++) {
            colour += "\""+ dataarea3.get(i).toString() +"\"" + ",";

        }
        return colour;
 }   

Upvotes: 2

pad
pad

Reputation: 239

If you need alle Strings in " just add \" in front of and an the end of each String.

static String[] convertToArray(List<String> list){
    String[] output = new String[list.size()];

    StringBuilder builder;
    for(int i=0;i<list.size();i++){
        builder = new StringBuilder();
        builder.append("\"");
        builder.append(list.get(i));
        builder.append("\"");
        output[i] = builder.toString();
    }

    return output;
}

Upvotes: 0

Alexander Kudrevatykh
Alexander Kudrevatykh

Reputation: 870

You can use org.apache.commons.lang.StringUtils.join or escape double quote

"\""

Also, don't use += on strings, use StringBuilder/StringBuffer for concatenating a lot of strings

Upvotes: 0

Amit Prajapati
Amit Prajapati

Reputation: 14315

ArrayList<String>  dataarea1= new ArrayList<String>();
dataarea1.add("ann");
dataarea1.add("john");
String[] mStringArray = new String[dataarea1.size()];
mStringArray = dataarea1.toArray(mStringArray);

for(int i = 0; i < mStringArray.length ; i++){
    Log.d("string is",(mStringArray[i]);
}

Upvotes: 0

Maxim Efimov
Maxim Efimov

Reputation: 2737

Use guava Iterables.toArray it's simple and efficient.

Upvotes: 0

Hybrid Developer
Hybrid Developer

Reputation: 2340

Consider this code to convert the ArrayList to String array

public void ConvertArrayListToarray()
        {
            ArrayList myArrayList = new ArrayList();
            myArrayList.Add("Jhon");
            myArrayList.Add("Jill");
            myArrayList.Add("Jo");
            myArrayList.Add("Chris");

            String[] myArray = (String[])myArrayList.ToArray(typeof(string));
            string str = string.Empty;
            for (int i = 0; i < myArray.Length; i++)
                           str += myArray[i] + "\n";
         }

Upvotes: 1

Related Questions