![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
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("");
}
}
} |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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] |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
so now how would i change the number in a random row and random column cause thats really what i need to do?
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#6 |
|
Newbie
|
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?"); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|