Would appreciate your help with 2d Array..
Hello Everyone,
I am new to Java and new to Java Forums. I would really appreciate it if someone can help me understand how I can do this task. I am learning java via online tutorials and couple of books. What I want to do is to populate a multidimensional array's elements with another arrays elements such as:
I declare two arrays:
1. moon[][] is multidimensional array
2. m in a array with assigned elements
I would like to take elements of Array m and assign them to the rows of the moon and then take the Array m elements again and multiply them by 3 and then assign them to the columns of Array moon. Any help would highly be appreciated .
Thank you.
Code:
public class 2dArray
{
public static void main( String args[] )
{
int[][] moon = new int [10][4];
int[] m = {1, 2, 3, 4, 5, 6};
for (int j=0; j < m.length; j++)
{
moon[j][j*3] = m[j]; // THIS IS WHERE I AM HAVING AN ISSUE
System.out.println(moon[j][j] + " " );
}
System.out.println();
// print array in rectangular form
for (int r=0; r < moon.length; r++)
{
for (int c=0; c < moon[r].length; c++)
{
System.out.print(" " + moon[r][c]);
}
System.out.println(" ");
}
}
}