逐行打印元素,以获取数组元素java的索引值

时间:2017-05-17 16:28:00

标签: java arrays

我正在迭代一个如图所示的数组,我想从下面的代码片段中逐行打印出单个元素

editted 数组是从数据库json响应中获取的,如图所示

// Extract data from json and store into ArrayList
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);
                        dataList.add(json_data.getString("nameo"));
                        dataList.add(json_data.getString("id"));
                    }

                    strArrData = dataList.toArray(new String[dataList.size()]);

通过响应

在此处迭代数组
for(int i = 0; i <strArrData.length; i++){
    System.out.println("populated data1>>>> "+ strArrData[i]); //this is prints out the array elements but not row by row
} 

请问如何逐行打印数组中的单个元素,例如

row 0 
item1 item2

row1
item3 item4

这样我就可以获得特定索引值的项目,例如

strArrData[2] will print item2 for instance

这是我目前的输出

populated data1>>>> Item1
populated data1>>>> 0
populated data1>>>> Item2
populated data1>>>> 1
populated data1>>>> Item3
populated data1>>>> 2

1 个答案:

答案 0 :(得分:0)

public class ClassvsInstance {

    private int[][] strArrData = new int[][]{{1,2,3},{3,4,5},{4,5,6}};

    public ClassvsInstance() {

        for (int i = 0; i < strArrData.length; i++) {
            System.out.print("row" + i + "\n"); // start new row 
            for (int j = 0; j < strArrData[i].length; j++) {
                System.out.print("\titem" + strArrData[i][j]);
            }
            System.out.println("\n"); // to new row 
        }
    }

    public static void main(String[] args) {
        new ClassvsInstance();

    }

输出:

row0
    item1   item2   item3

row1
    item3   item4   item5

row2
    item4   item5   item6