Copy an array from 1D to 2D

4 posts / 0 new
Last post
heartin
Copy an array from 1D to 2D

Write a program to copy a 1-dimension array with 12 elements into a 2-dimensional array with 3x4 elements. Print the 2-D array as a matrix.

Note

If someone has already given an answer for above problem, try other combinations like a 1-dimension array with 8 elements into a 2-dimensional array with 2x4 elements.

Hint

  1. You can directly initialize a 1-D array and then use a for loop to copy. 1-D array has only one index as arr1[i] and 2-D array has two indexes as arr2[j][k].
  2. To print the 2-D array as a matrix, use two for loops. Outside loop is for rows and inner one for col. Inner one will be executed for all rows. 

 

ALL THE BEST!!!

Was it useful?
sneha
Copying elements from ID to 2D array

This program has been rewritten again below with heading 'Modified Program after Review'. Retaining the original program for reference.

package com.javajee.Utils;

public class CopyArray {
    
    int[] arr1={1,2,3,4,5,6,7,8,9,10,11,12};
    int rowSize=3;
    int columnSize=4;
    int[][] arr2=new int[rowSize][columnSize];

    public static void main(String[] args) {
        CopyArray CA=new CopyArray();
        CA.copyElems(CA.arr1);
        CA.printElem(CA.arr2);

    }
    
    public void copyElems(int[] A){
        int k=0;
        for(int i=0;i<rowSize;i++){
            for(int j=0;j<columnSize;j++){
                arr2[i][j]=A[k];
                k++;
            }
        }
        
    }
    
    public void printElem(int[][] B){
        for(int i=0;i<rowSize;i++){
            for(int j=0;j<columnSize;j++){
                System.out.print(B[i][j]+" ");                
            }
            System.out.println();
        }
    }

}
 

Was it useful?
heartin
Good, but can be better...

Here arr1 is an instance variable. So should we really have to pass it into copyElems function? If so, why not sending arr2?

It would be great if you can try to make these methods indpendent of static / instance variables so that it can be reused in other places.

NOTE:

  1. If rewriting, please write a new one with a heading like 'Modified Program after Review'.
  2. In the original program, add a note on top that the program has been rewritten.
Was it useful?
sneha
Modified Program after Review

package com.javajee.Utils;

public class CopyArray {    

    public static void main(String[] args) {
        int[] arr1={1,2,3,4,5,6,7,8,9,10,11,12};
        int[][] arr2=new int[3][4];        
        copyElems(arr1,arr2);
        printElem(arr2);

    }
    
    public static void copyElems(int[] A,int[][] B){
        int k=0;
        for(int i=0;i<B.length;i++){
            for(int j=0;j<B[0].length;j++){
                B[i][j]=A[k];
                k++;
            }
        }
        
    }
    
    public static void printElem(int[][] B){
        for(int i=0;i<B.length;i++){
            for(int j=0;j<B[0].length;j++){
                System.out.print(B[i][j]+" ");                
            }
            System.out.println();
        }
    }

}
 

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (8) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (12) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)