This discussion is archived
1 2 Previous Next 19 Replies Latest reply: Jun 28, 2007 2:31 AM by 800322 RSS

2D array to 1D array conversion????

807600 Newbie
Currently Being Moderated
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
public class ArrayConversion {

     
     public static void main(String[] args) {
          int i,j,flag=0;
          int count=10;
          int ar[][]=new int[2][3];
          int arCopy[]=new int[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);
               System.out.print(" ");
               
          }
     }

}

  • 1. Re: 2D array to 1D array conversion????
    800322 Newbie
    Currently Being Moderated
    [del]
  • 2. Re: 2D array to 1D array conversion????
    807600 Newbie
    Currently Being Moderated
    plz do reply thank you!!
  • 3. Re: 2D array to 1D array conversion????
    800282 Newbie
    Currently Being Moderated
    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.
  • 4. Re: 2D array to 1D array conversion????
    807600 Newbie
    Currently Being Moderated
    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???
  • 5. Re: 2D array to 1D array conversion????
    807600 Newbie
    Currently Being Moderated
    that would be a valid solution, assuming that your 2d array is just another data container.
  • 6. Re: 2D array to 1D array conversion????
    800282 Newbie
    Currently Being Moderated
    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?
  • 7. Re: 2D array to 1D array conversion????
    807600 Newbie
    Currently Being Moderated
    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!!!
  • 8. Re: 2D array to 1D array conversion????
    800282 Newbie
    Currently Being Moderated
    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?
  • 9. Re: 2D array to 1D array conversion????
    807600 Newbie
    Currently Being Moderated
    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.
  • 10. Re: 2D array to 1D array conversion????
    800322 Newbie
    Currently Being Moderated
    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.length ...) {
    // inserting of x[i][j] here
    }
    }
  • 11. Re: 2D array to 1D array conversion????
    800282 Newbie
    Currently Being Moderated
    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;
    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.
  • 12. Re: 2D array to 1D array conversion????
    807600 Newbie
    Currently Being Moderated
    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??
  • 13. Re: 2D array to 1D array conversion????
    800282 Newbie
    Currently Being Moderated
    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!
  • 14. Re: 2D array to 1D array conversion????
    807600 Newbie
    Currently Being Moderated
    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!
1 2 Previous Next