Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-11-2008, 06:17 AM
Member
 
Join Date: Feb 2008
Posts: 1
cloudkicker is on a distinguished road
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.

PHP Code:

public class 2dArray
{

          public static 
void mainString args[] )
     {


      
int[][] moon = new int [10][4];
      
int[] = {123456};

      for (
int j=0m.lengthj++)
         {
            
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=0moon.lengthr++)
             {
                 for (
int c=0moon[r].lengthc++)
                 {
                     
System.out.print(" " moon[r][c]);
                 }
                 
System.out.println(" ");
             }

      }



Last edited by cloudkicker : 02-11-2008 at 06:36 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-11-2008, 03:34 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Hi,

fisrst, how do you like to assign 6 numbers to your moon arrray ?
moon looks like this

? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?

you have just 6 numbers, so you can do

1 2 3 4 5 6 ? ? ? ?
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?
Is it what you want?

next,
to get an element from 2d array, you need to indicate both dimensions: moon[?][?], for instance you need to do moon[0][1]to get an element from the last array
Therefore, you need to use two loops to get each element in the 2d array (unless you wanna do it as in the previous array, if you do, you need to indicate, for example, first dimension as 0)

next, you write:
moon[j][j*3] = m[j];
when you do it that way, you get element, which is located on the crossing of the j raw and j*3 column, this is obviously not what you want to do,
again,you need to use two loops and two variables (i,j) to track all the elements, second to multiply element, you will need to do this
moon[i][j]=m[i]*3;

I am not sure if I understood your idea, but these are some comments

Last edited by hey : 02-11-2008 at 03:36 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array Help bluegreen7hi New To Java 2 03-28-2008 03:25 AM
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 09:59 PM
2D array bluekswing New To Java 2 01-15-2008 06:57 PM
Bounded Array bugger New To Java 4 01-04-2008 10:41 AM
Help with Array susan New To Java 1 08-07-2007 05:32 AM


All times are GMT +3. The time now is 11:20 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org