2D array to 1D array conversion?
how can i convert a 2D array to a 1D array.... i have written some code which copies a 2d aray to a 1d array... but is this enough.. i mean can i say this cide as conversion of 2d array to 1d array.. or just copying.. and how to convert a 2d to 1d array?
here is the code
publicclass ArrayConversion{
publicstaticvoid main(String[] args){
int i,j,flag=0;
int count=10;
int ar[][]=newint[2][3];
int arCopy[]=newint[6];
for(i=0;i<2;i++){
for(j=0;j<3;j++){
ar[i][j]=count;
count++;
}
}
for(i=0;i<2;i++){
for(j=0;j<3;j++){
System.out.print(ar[i][j]);
System.out.print(" ");
}
System.out.println(" ");
}
for(i=0;i<2;i++){
for(j=0;j<3;j++){
arCopy[flag]=ar[i][j];
flag++;
}
}
System.out.println(" ");
for(i=0;i<arCopy.length;i++){
System.out.print(arCopy[i]);
System.out.print(" ");
}
}
}
>
Break it up into methods. Really.
Here's a way to do it:
public class Foo {
public static int[] twoDimToOneDim(int[][] twoDim) {
int[] oneDim = new int[numberOfElements(twoDim)];
// Loop through the 2d array and place each element in the 1d array
return oneDim;
}
private static int numberOfElements(int[][] twoDim) {
int number = 0;
// Loop through the 2d array and count how many element it contains.
return number;
}
public static void main(String[] args) {
int[][] twoDim = {{1,2,3,4},{5,6},{7,8,9}};
int[] oneDim = twoDimToOneDim(twoDim);
System.out.println(java.util.Arrays.toString(oneDim));
}
}
Try to fill in the blanks.
thanks alot... well i will format it accordingly.... but is this code is what we can say as conversion of 2D array 1D array...?i mean the conecpt of my code is alrite ? it conversion only .na and not jus copying?
that would be a valid solution, assuming that your 2d array is just another data container.
> thanks alot... well i will format it accordingly....
> but is this code is what we can say as conversion of
> 2D array 1D array...?i mean the conecpt of my
> code is alrite ? it conversion only .na and not jus
> copying?
Sorry, I really can't read that. How will you be able to write code if you can't properly spell words and use normal punctuation?
iam really sorry.. i have already been warned here... iam trying to improve writting... well it;s all because of chatting ,,, but will try and improve more.. sorry once again!!
Now i was asking that .. the code i have written or you have given .. is what we call converison right? it is not just copying but conversion ... right?i mean if some one asks for a 2d to 1d array conversion then this code is what we should give them?
and can you please give me the full code.. i mean how to loop thorugh and count variable and how to assign them to oneDim array.. if you can provide will really help thanks alot!!!
> iam really sorry.. i have already been warned here...
> iam trying to improve writting... well it;s all
> because of chatting ,,, but will try and improve
> more.. sorry once again!!
- a new sentence starts with a capital (A,B,C, ... , Z);
- punctuation marks (.,!?) are usually typed only once, not a lot of them in succession, like this ..........
> Now i was asking that .. the code i have written or
> you have given .. is what we call converison right?
> it is not just copying but conversion ...
> right?i mean if some one asks for a 2d to 1d
> array conversion then this code is what we should
> give them?
I did not look too closely at your code. It's hard to follow and it will only work for arrays of 2*3. Every time you're changing the size of the 2D array, you will have to modify your algorithm: this is not good!
> and can you please give me the full code.. i mean how
> to loop thorugh and count variable and how to assign
> them to oneDim array.. if you can provide will really
> help thanks alot!!!
Why don't you try it yourself?
I just wanted to confirm about 2d and 1d arrays.Regarding trying myself iam a bit confused about looping through and assigning variables as we already know how many elements we have in our 2d array then why count number?
Also i am not getting how to loop and put 2d array contents in 1d array, i mean do i have to put in 2 loops, then what should be the limits for them i mean .lenght method cannont be called then wat should be the value for both the loops for the ending condition of loops. Hope iam clear.
PS-Thanks alot for your advice,i wil surely try to improve my writing even more.
> Now i was asking that .. the code i have written or
> you have given .. is what we call converison right?
> it is not just copying but conversion ...
> right?i mean if some one asks for a 2d to 1d
> array conversion then this code is what we should
> give them?
If someone asks you that, you should ask *him* what he actually wants. It depends on whether *he* calls it conversion.
Edit: no, you're not clear.
Object[][] x = ...
for (... i < x.length ...) {
// ...
for (... j < x[i].length ...) {
// inserting of x[i][j] here
}
}
> I just wanted to confirm about 2d and 1d
> arrays.Regarding trying myself iam a bit confused
> about looping through and assigning variables as we
> already know how many elements we have in our 2d
> array then why count number?
It's "better" to write a more general method for handling this. What if your requirements change and the 2D array expands into something larger?
> Also i am not getting how to loop and put 2d
> array contents in 1d array, i mean do i have to put
> in 2 loops, then what should be the limits for them
> i mean .lenght method cannont be called then wat
> should be the value for both the loops for the
> ending condition of loops. Hope iam clear.
Ok, here's a little demo to loop through a 2D array:
int[][] twoDim = {{1,2,3,4},{5,6},{7,8,9}};
int oneDimIndex = 0;
for(int i = 0; i < twoDim.length; i++) {
int[] row = twoDim[i];
for(int j = 0; j < row.length; j++) {
System.out.println("twoDim["+i+"]["+j+"] = "+row[j]+
", should be at oneDim["+oneDimIndex+"]");
oneDimIndex++;
}
}
Try to incorporate it into what I have posted earlier. But first run this in a single main-method and understand how it works.
> PS-Thanks alot for your advice,i wil surely try to
> improve my writing even more.
Thank you for your understanding. It really is much better like this!
Good luck.
thanks alot prometheuzz and CeciNEstPasUnProgrammeur , i got it working and also understood how it works, well i havent read the array class yet thats why was getting problem but now it;s all fine.Thanks again and just one question when we declare a 2D array suppose twoDim and then call the twoDim.length, then wat value is assigned to the lenght, the rows value or the cloumn?
> thanks alot prometheuzz and
> CeciNEstPasUnProgrammeur , i got it working and
> also understood how it works, well i havent read the
> array class yet thats why was getting problem but now
> it;s all fine.
You're welcome.
> Thanks again and just one question when
> we declare a 2D array suppose twoDim and then call
> the twoDim.length, then wat value is assigned to the
> lenght, the rows value or the cloumn?
They're not really called rows and columns, but it's easy to remember them like that.
twoDim.length -> rows
twoDim[i].length -> number of elements in a row at index 'i' (columns), it can be different per row!
> They're not really called rows and columns, but it's
> easy to remember them like that.
> twoDim.length -> rows
> twoDim[i].length -> number of elements in
> a row at index 'i' (columns), it can be different per
> row!
Thanks again well i have almost understood it and now when i'll go through the chapter regarding arrays i will be able to understand very well. I must say this place is the best!
Cheers to all!
one more question, now i got the point about 2D arrays but wats going to be the format for a 3D array,
for a 2D array the procedure is this
Object[][] x = ...
for (... i < x.length ...) {
// ...
for (... j < x[i].length ...) {
// inserting of x[i][j] here
}
}
how is it done if it was a 3D array?
Object[][] x = ...
for (... i < x.length ...) {
// ...
for (... j < x[i].length ...) {
for (... k < x[i][j].length ...){
// inserting of x[i][j][k] here
}
}
}
Is this code correct?
You could create a small example and try it... (Yes, it is.)
thank you very much, iam about to start the chapter for Arrays so will try and see everything practically., and if some doubts, then i have wonderful people like you.. thanks again please always be on this forum to help us beginner's to move ahead .. cheers to you and all!!