Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 7th, 2005, 2:45 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
multi-demensional arrays

I need to write a 2 deminsional array in java. I can display the array fine with all the initialized to 0 but how do i change the number of a specific coordinates
here is the array now:
public class Board
{
   
   public static void main(String args[])
   {
      int[][] ar = new int[10][10];
      Board.setBoard(ar);
   }
    
    
    public static void setBoard(int[][] ar)
    {
       for (int r=0; r<ar.length; r++)
       {
            for (int c=0; c<ar[r].length; c++)
            {
                System.out.print(" " + ar[r][c]);
            }
           System.out.println("");
      }
    }
}
cwl157 is offline   Reply With Quote
Old Apr 7th, 2005, 2:52 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Ok, so basically, right now your array looks something like this:

 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0

Now, lets say, you wanted to change the value in the 5th row and 5th column to 5, you'd say something like this:

ar[4][4] = 5;

Now your array will look like this:

  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 5 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 8th, 2005, 2:41 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
If that doesnt help draw the grid on a piece of paper, just think of it as coordiantes

(5,2) and so that would be myArray[5][2]
Berto is offline   Reply With Quote
Old Apr 14th, 2005, 7:44 AM   #4
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
so now how would i change the number in a random row and random column cause thats really what i need to do?
cwl157 is offline   Reply With Quote
Old Apr 14th, 2005, 10:22 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
generate a random number within the bounds of your row index [R] and another one in the bounds of your column index [C]. Then make the assignment...

ar[R][C] = yourVal;

I wrote something like this in C++ for the "Game of Life" when i was in college.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 28th, 2005, 12:03 AM   #6
You Like Java?
Newbie
 
You Like Java?'s Avatar
 
Join Date: Mar 2005
Location: aboho.com (sn:eyvind)
Posts: 20
Rep Power: 0 You Like Java? is on a distinguished road
Send a message via AIM to You Like Java? Send a message via MSN to You Like Java? Send a message via Yahoo to You Like Java?
public class LikeThis
{
   public static void main(String[] args)
   {
	 //example data
	 int[][] array = new int[5][10];
	 double value = 23.5;
 
	 /*
	 create the Random object with the
	 time in miliseconds as the seed
	 */
	 long seed = new Date().getTime();
	 Random randNumGen = new Radnom(seed);
 
	 //get the coordinates
	 int x = randNumGen.nextInt(5);
	 int y = randNumGen.nextInt(10);
 
	 //access and modify the array
	 array[x][y] = value;
   }
}
__________________
If you need help with Java, you can go to javaforum.tk and get answers quickly.

Programmer YouLikeJava = new JavaProgrammer("Eyvind");
YouLikeJava.printInfo();
YouLikeJava.setWeb("You Like Java?");
You Like Java? is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC