Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Please help me how to convert 2-d array to 1-d array in java :( here's the code

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Please help me how to convert 2-d array to 1-d array in java :( here's the code

    import java.io.*;
     public class Program2 {
     public static void print_array(int[][] numbers)
    {
     
     for (int i=1; i<numbers.length; i++) {
     System.out.print("" + i + " |");
     for (int a=1; a<numbers[i].length; a++)
     System.out.print("\t" + numbers[i][a]);
     System.out.println(); }
     
    }
     
    public static void main(String[] args) throws IOException {
    BufferedReader r = 
    new BufferedReader (new InputStreamReader(System.in));
     
    String yesno="u";
    boolean isExit=false;
     
    do{
     
    String row, column;
    int Columns = 0;
    int Rows = 0;
    System.out.print("\n");
    System.out.print("ENTER A NUMBER : ");
    row= r.readLine();
    Rows= Integer.parseInt( row );
     
     
    int values[][] = new int[Rows+1][Rows+1];
    int [][] m = new int[Rows][];
    for (int i=0; i < Rows; i++)
    m[i] = new int[Rows];
    for (int i=0; i < values.length; i++)
    for (int a=0; a < values[i].length; a++)
    values[i][a] = i*a;
    System.out.print("\n");
    for (int i=0; i < values.length; i++){
    System.out.print(" " + i + " ");
    }
    System.out.println();
    for (int i=0; i < values.length; i++){
    System.out.print("" + "" + "---");
    }
    System.out.println();
    print_array(values);
    System.out.print("\n\n");
     
      System.out.println("Try again (press 1 if yes/press 2 if no)?");
    	yesno = r.readLine();
     
    		if(yesno.equalsIgnoreCase("1")) {
    		    isExit=false;
    		} else if(yesno.equalsIgnoreCase("2")) {
    		    isExit=true;
    		}	
     
    		}while(!isExit);
    		 System.out.print("Goodbye!");
     
     
     }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Please help me how to convert 2-d array to 1-d array in java :( here's the code

    More information needed. What's the problem? What have you tried? Why are you doing this?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Please help me how to convert 2-d array to 1-d array in java :( here's the code

    ...and suggested reading: How to Help Yourself Get Help

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me how to convert 2-d array to 1-d array in java :( here's the code

    this is a multiplication table 2d array, can you convert it to 1d array? this is my problem.. i don't how to convert it to 1d array

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Please help me how to convert 2-d array to 1-d array in java :( here's the code

    Do you mean just take all the values and stick them in one array?

    Get the total size of the 2D array by doing length*width
    int[][] tda = new int[5][5];
    int total = tda.length*tda[0].length;
    int[] oda = new int[total];

    Then iterate through and add.

    int index = 0;
    for(int[] a:tda) {
        for(int i:a) {
            oda[index] = i;
            index++;
        }
    }

    Sorry if I messed any syntax or logic up. I whipped that up on the spot, so it might have errors. Let me know and I will come back and fix it.

    It should work though.

Similar Threads

  1. I need to convert String into array
    By talha07 in forum Collections and Generics
    Replies: 1
    Last Post: October 9th, 2011, 01:58 PM
  2. Array month/day/hour/min/sec incomplete java code
    By mightyking in forum Collections and Generics
    Replies: 12
    Last Post: September 18th, 2011, 08:46 PM
  3. How Convert a Byte array to a image format
    By perlWhite in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 19th, 2011, 02:16 PM
  4. convert vector to array of bytes
    By chopficaro in forum Java Theory & Questions
    Replies: 1
    Last Post: May 3rd, 2010, 11:00 AM
  5. Convert Vector to Byte Array
    By perlWhite in forum Java Theory & Questions
    Replies: 0
    Last Post: August 25th, 2009, 05:45 AM