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 3 of 3

Thread: copying numbers stored in one 2d array into a new 2d array with different dimensions.

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default copying numbers stored in one 2d array into a new 2d array with different dimensions.

    Here is the problem:

    Write a program that creates a 2 dimensional array X containing 4*6 elements. Store numbers from 1-24 in the array. Create another array Y that can store 3*8 elements. Write a program that copies all the numbers from X to Y. Print both arrays.

    This is what i have so far but the trick is i am not supposed to just create the new array with dimensions 3x8 i am supposed to copy the numbers from the 4x6 array into the new 3x8 array:

    public class c2 {public static void main(String[]a){
     
    	int [][]x=new int [4][6]; 
     
    	int temp=1; 
    	for(int i=0;i<=3;i++){ 
    	for (int j=0;j<=5;j++){ 
    	x[i][j]=temp;
    	temp=temp+1;
     
    		}
     
     
    		}
    	int [][]y=new int[3][8];
    	int temp1=1;
    	for(int b=0;b<=2;b++){ 
    		for (int d=0;d<=7;d++){ 
    			y[b][d]=temp1;
    			temp1=temp1+1;
    		}
    	}
    	for(int i=0;i<=3;i++){ 
    		for (int j=0;j<=5;j++){ System.out.print(" " +x[i][j]);}
    		System.out.println();
     
     
    	}
    	System.out.println();
    	for(int b=0;b<=2;b++){ 
    		for (int d=0;d<=7;d++){System.out.print(" " + y[b][d]);}
    		System.out.println();
     
    	}
     
     
    }
    }
    Last edited by helloworld922; April 18th, 2011 at 08:08 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: copying numbers stored in one 2d array into a new 2d array with different dimensi

    Please don't post multiple threads with the same question. I already wasted time responding to your other one- which I've now deleted.

    I'd suggest you draw out both arrays, labeling each index. That way you can see how they correspond to one another.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: copying numbers stored in one 2d array into a new 2d array with different dimensi

    An easy enough solution of the top of the head would be to take the contents of the first array and store them in a temporary 1 dimensional array. Then transfer them over to your new array. Probably an easier way but its the first thing I thought of. I just threw the code into a pair of methods.

    class Copy2dArray
    {
    	//Copies the contents of a 2d array into another 2d array
    	//@PARAM The array to be copied and the two values determining the size of
    	//the M * N sized array to be created. mDim = M, nDim = N.
    	static int[][] copy2dArrayContents(int[][] original, int mDim, int nDim)
    	{
    		int[] arrayTemp = new int[mDim*nDim]; //nDim * mDim is the max amount of elements that the copy can store, as specified by user
    		int[][] copy = new int[mDim][nDim]; //array with specified dimension to store the copied contents
    		int tempIndex = 0;
     
    		//Copy array1 contents into arrayTemp
    		for(int i = 0; i < original.length; i++)
    		{
    			for(int j = 0; j < original[i].length; j++)
    			{
    				arrayTemp[tempIndex] = original[i][j];
    				tempIndex++;
    			}
    		}
    		tempIndex = 0;
     
    		for(int i = 0; i < copy.length; i++)
    		{
    			for(int j = 0; j < copy[i].length; j++)
    			{
    				copy[i][j] = arrayTemp[tempIndex];
    				tempIndex++;
    			}
    		}
     
    		return copy;
     
    	}
     
    	//print out a 2d array to STD output
            //@PARAM The array to be printed
    	static void print2dArray(int[][] array)
    	{
    		for(int i = 0; i < array.length; i++)
    		{
    			for(int j = 0; j < array[i].length; j++)
    			{
    				System.out.print(array[i][j] + " ");
    			}
    		}
    		System.out.println("");
    	}
     
    	public static void main(String[] args) 
    	{
    		//Declare variables
    		int[][] array1 = new int[4][6]; //Original array
    		int[][] array2 = new int[3][8]; //Array to be copied to
    		int start = 1;
     
    		//Initialize array1 with numbers 1 - 24
    		for(int i = 0; i < 4; i++)
    		{
    			for(int j = 0; j < 6; j++)
    			{
    				array1[i][j] = start;
    				start++;
    			}
    		}
     
    		//Copy array1 contents into array2
    		array2 = copy2dArrayContents(array1, 3, 8);
     
    		//print both arrays to standard output
    		print2dArray(array1);
    		print2dArray(array2);
    	}
    }

    Resulting Output:
    L:\Code\Java\java Copy2dArray
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    Last edited by TopdeK; May 6th, 2011 at 07:11 PM.

Similar Threads

  1. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 12:01 PM
  2. selecting even numbers from an array
    By Tate in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 29th, 2010, 08:03 AM
  3. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM
  4. random numbers in array please help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 01:54 AM

Tags for this Thread