Assigning list index values to String I am just trying to convert each list of values to String by index one by one. My Code is running fine. But I am thinking that I can write better code than this. I Need your good coding possibilities here to convert from list to String.

I am using the code below to convert:

CarComment[] list = form.getCarDataJaxb().getCarCommentList();
JSONObject obj = new JSONObject();
JSONArray comments = new JSONArray();
for (int i=0;i&lt;list.length;i++){
    if (notEmpty(list[i].getText())){
        JSONArray commObj = new JSONArray();

        String str = list[i].getText();
        str = str.replaceAll("&lt;", "&amp;lt;");
        commObj.put(str);

        comments.put(commObj);
    }
}
obj.put("carcomm", comments);
response.setContentType("text/json");
response.getWriter().write(obj.toString());


I am trying to write good coding technique while converting from list to String at line
String str = list[i].getText();
There actually isn't so much to improve here (at least not that I can think of).

Instead of using an index to iterate by, you can use a Java for-each loop. Since you only used the iterator variable to read the index of the list, it is usually more readable to use a for-each loop.

You were not indenting your code correctly (before the edit of your question), which I also fixed below.

And, below I use the opposite way of checking if the text is empty, by using continue if the if-statement is not true, you can avoid having to indent the code an extra step.

CarComment[] list = form.getCarDataJaxb().getCarCommentList();
JSONObject obj = new JSONObject();
JSONArray comments = new JSONArray();
for (CarComment carComment : list) {
    if (!notEmpty(carComment.getText()))
       continue;
    JSONArray commObj = new JSONArray();
    commObj.put(carComment.getText().replaceAll("&lt;", "&amp;lt;"));
    comments.put(commObj);
}
obj.put("carcomm", comments);
response.setContentType("text/json");
response.getWriter().write(obj.toString());


Preferably, to avoid double-negation, you should use if (isEmpty(...)) instead of if (!notEmpty(...))

However, there's no way of having to do a method call to getText (or a similar method call) on your line.

String str = list[i].getText();


What I did was to skip the declaration of a local variable and process the string (by calling replaceAll) directly when adding it to it's destination.