0

Okay so I have successfully turned a list into a two dimensional array. The only problem is the output only indexes it once, so basically if I have 10 elements within each list that I want to add to a two dimensional array, the two dimensional array will have only one index with 'n' number of elements.

For example

I would like

{{1,2,3}, {4,5,6}, {7,8,9}}

Instead it is returning:

{1,2,3,4,5,6,7,8,9}

I took suggestions from: Convert ArrayList into 2D array containing varying lengths of arrays

Here is my code:

public static Object[][] getOrderCreateTestCases(){
    List<List<String>> list = new ArrayList<>();
    List<String> values = new ArrayList<>();
    try {
        JSONArray jObject = (JSONArray)getClient().sendGet(String.format("get_cases/12&suite_id=136"));
        for(Object obj : jObject){
            JSONObject jObj = (JSONObject)obj;
            values.add(jObj.get("title").toString());
            values.add(jObj.get("id").toString());
            values.add(jObj.get("custom_order_type").toString());
            values.add(jObj.get("custom_product_type").toString());
            values.add(jObj.get("custom_free_shipping").toString());
            values.add(jObj.get("custom_billing_country").toString());
            values.add(jObj.get("custom_shipping_country").toString());
            list.add(values);
            for(int i=0; i<list.size(); i++){
                valuesString = new Object[list.get(i).size()][];
                List<String> row = list.get(i);
                valuesString[i] = row.toArray(new String[row.size()]);
                //System.out.print(valuesString[i]);
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (APIException e) {
        e.printStackTrace();
    }
    return valuesString;
}

I am working with DataProviders with TestNG and they require a return of a two dimensional Object array, which I can understand why. I am parsing out certain data from a JSON Array (json-simple), adding it to the list, and then converting to a two dimensional array. So let's say it grabs the info from ID=5546, then the next id=4987, next id=3847 and so on.. Any help would be greatly appreciated

UPDATED...

Okay so I think I see why it's doing what it's doing but I still do not know how to solve the problem. So basically as it loops and it begins the new set of data, then it needs to create a new array.

{{List1}, {List2}, {List3}}

Community
  • 1
  • 1
user2474976
  • 87
  • 3
  • 13

1 Answers1

1

I suppose getOrderCreateTestCases is your data provider. If so could you try it this way?

public static Object[][] getOrderCreateTestCases() {
    List<List<String>> list = new ArrayList<>();
    List<String> values = new ArrayList<>();

    try {
        JSONArray jObject = (JSONArray) getClient().sendGet(
                String.format("get_cases/12&suite_id=136"));

        for (Object obj : jObject) {

            try {

                JSONObject jObj = (JSONObject) obj;
                values.add(jObj.get("title").toString());
                values.add(jObj.get("id").toString());
                values.add(jObj.get("custom_order_type").toString());
                values.add(jObj.get("custom_product_type").toString());
                values.add(jObj.get("custom_free_shipping").toString());
                values.add(jObj.get("custom_billing_country").toString());
                values.add(jObj.get("custom_shipping_country").toString());
                list.add(values);
            } catch (Exception e) {
                // Ignore
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (APIException e) {
        e.printStackTrace();
    }
    valuesString = new Object[list.get(i).size()][];

    for (int i = 0; i < list.size(); i++) {
        List<String> row = list.get(i);
        valuesString[i] = row.toArray(new String[row.size()]);
    }

    return valuesString;
}
Sachin Francis
  • 1,071
  • 9
  • 9