convert java arraylist to array using for loop

919 views Asked by At

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

3

There are 3 answers

5
Siddharth Kumar On BEST ANSWER

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.

1
Yuri Stiopin On

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
}
0
Vidhya On

I am not sure if we can do that using for loop. But if you want one without for loop. We can do:

    ArrayList<Integer> arrayList5=new ArrayList<Integer>();
    arrayList5.add(1000000);
    arrayList5.add(2000000);
    
    //the final array created with the help of toArray
//is of Object type hence create an array first and then pass it as argument
        Integer[] array5=new Integer[arrayList5.size()];
        arrayList5.toArray(array5);
        
//print the array
        for(Integer i: array5) {
            System.out.println(i);
        }