convert java arraylist to array using for loop

流过昼夜 提交于 2019-12-25 08:32:43

问题


I have viewed the many similar topics here, none have allowed me to resolve:

 //how to Convert this arraylist to array string using loop

public static void main(String[] args) 
{   
Main m = new Main();
List<JSONObject> jObj = m.getJsonObject();
for (int i = 0; i < jObj.size(); i++) 
    {
    System.out.println(jObj.get(i));
    }
 }  

The many examples I have seen all create an array list by adding in the code, I already have the arrayList. I just need to modify it as an array string and not an arrayList so that I may properly format it as a JSON string with

String responseStr = "{\"data\":" + 

Here is the entire bean :

package com.queryData.main;

import com.queryData.dao.DataDAO;
import com.queryData.services.JsonServices;
import java.sql.ResultSet;
import java.util.List;
import org.json.JSONObject;

public class Main {
    ResultSet resultSet = null;
    DataDAO datadao = new DataDAO();

    public List<JSONObject> getJsonObject()
    {
        resultSet = datadao.getResultSet();
        List<JSONObject> resList = JsonServices.getFormattedResult(resultSet);      
        return resList;
    }
    //how to Convert this arraylist to array string using loop

    public static void main(String[] args) 
    {   
    Main m = new Main();
    List<JSONObject> jObj = m.getJsonObject();
    for (int i = 0; i < jObj.size(); i++) 
        {
        System.out.println(jObj.get(i));
        }
     }
}

I tried the following code, but it does not work.

package com.queryData.main;

import com.queryData.dao.DataDAO;
import com.queryData.services.JsonServices;
import java.sql.ResultSet;
import java.util.List;
import org.json.JSONObject;

public class Main {
    ResultSet resultSet = null;
    DataDAO datadao = new DataDAO();

    public List<JSONObject> getJsonObject()
    {
        resultSet = datadao.getResultSet();
        List<JSONObject> resList = JsonServices.getFormattedResult(resultSet);      
        return resList;
    }
    //how to Convert this arraylist to array string using loop

    public static void main(String[] args) 
    {   
    Main m = new Main();
    List<JSONObject> jObj = m.getJsonObject();
    for (int i = 0; i < jObj.size(); i++) 
        {
        //System.out.println(jObj.get(i));
        }

    String[] stringArr = jObj.toArray( new String[] {} );
    for ( String element : stringArr ) {
        System.out.println( element );


     }
    }   
}

Any suggestions greatly appreciated. Thanks


回答1:


Use StringBuilder to build String :

List<Object> jObj = m.getJsonObject();
StringBuilder sb = new StringBuilder();
for(int i =0 ; i < jObj.size(); i++){
   sb.append(jObj.get(i).toString());
}

Finally sb.toString() will be your desired String.




回答2:


you can make some changes : List supports trick like

List<Object> jObj = m.getJsonObject();
for(int i =0 ; i < jObj.size(); i++){
   jObj.add(jObj.get(i).toString(), i); // change toString() to whatever u need
}


来源:https://stackoverflow.com/questions/41415865/convert-java-arraylist-to-array-using-for-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!