user2097111
user2097111

Reputation: 49

2Darray to 1darray with for loop

I know this is a very basic question, but I'm just new to programming and I think I'am struggling with a very simple issue... but here we go :p

In my app I have an array of arrays like this storing data retrieved from a mysql database.

data_array[][]

0001 | data1 | data2 | data3 | data4 | data5
0002 | data1 | data2 | data3 | data4 | data5
...
... and so on. 

Now in my app, I need to retrieve for each of the rows the first of the colums, ending with something like :

array { 0001, 0002}

I know doing ' for iterations ' I can retrieve all the data:

public String[] itarray{
int cols = 5;
String[] xFINAL;
for (int i=0 ; i < data_array; i++) {
    for (int j=0; j < cols; j++){
        xFINAL = data_array[i];
        System.out.println("1" + data_array[i][0]);

    }

System.out.println(" ");
} return xFINAL;}

But how can I get the first column for each of the rows? I tried with something like:

int j=0;
for (int i = 0; i < data_array ; i++){
 xFINAL[j] = data_array [i][0];
j++;
}

But it's giving me a null pointer. How could I do this please?

Thanks in advance.

Upvotes: 0

Views: 57

Answers (1)

Achintya Jha
Achintya Jha

Reputation: 12843

String[] xFINAL = new String[data_array.length];   

int k =0;
for (int i=0 ; i < data_array; i++) {
    array[k++] = data_array[i][0];


}

Upvotes: 1

Related Questions