Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Hawtrey

Help with Java code-example included

This topic is 5726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s some stopid code for a stoopid thing I was messing around with. import java.util.*; class ArrayClass { String [] [] contents =new String[9][9]; public void fill(){ for (int i=0;i<9;i++){ for (int j=0;j<9;j++) {contents[j]= "A";}}} public void print(){ for (int i=0;i<9;i++){ for (int j=0;j<9;j++) {System.out.print(contents[i][j]);if (j==8) System.out.println (" "); }}} public void mutate() { Random rand = new Random(); int range=9; int x; int y; for (int mute=0;mute<6;mute++){ x= (rand.nextInt(range));y=(rand.nextInt(range)); contents[x] [y] = "*";} } public String [][] getarray() { return contents ; } public static void main(String[] arguments) { ArrayClass poop = new ArrayClass(); poop.fill(); String [] [] temp = new String [9][9]; !!!!temp = poop.getarray(); poop.contents[5][5]="7"; poop.mutate ();!!!! for (int i=0;i<9;i++){ for (int j=0;j<9;j++) {System.out.print(temp[i][j]);if (j==8) System.out.println (" "); }} Section marked with !!!! is the bit that seems fishy. What the whole program should do is create and arrayclass with a multidim array that gets filled with ''A'' and the mutate function changes 6 of the A''s into "*".In the void main a new array (temp) is created and and the final bit should print out the array (temp) in a little 9,9 square. Only trouble is that it seems to print it with the mutated "*"''s which it shouldn''t. Mutate is called after getarray...

Share this post


Link to post
Share on other sites
Advertisement
Firstly, ArrayClass is not a standard class, so assumptions are in order..

String [] [] temp = new String [9][9];
temp = poop.getarray();

The first of these is almost redundant - you ''overwrite'' your initial 9x9 array by whatever getArray returns. You need to look at the behaviour of getArray() but chances are it''ll return a reference to the contents, not a copy. Hence, both temp and poop are pointing to the same data.

Share this post


Link to post
Share on other sites
OrangyTang is correct. All you are doing is getting temp to point to the same object as contents. You need to copy contents into temp instead. Like this:

  
import java.util.*;

public class ArrayClass {
String [] [] contents =new String[9][9];

public void fill() {
for (int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
contents[i][j]= "A";
}
}
}

public void print() {
for (int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
System.out.print(contents[i][j]);
if (j==8) {
System.out.println (" ");
}
}
}
}

public void mutate() {
Random rand = new Random();
int range=9;
int x;
int y;
for (int mute=0;mute<6;mute++) {
x= (rand.nextInt(range));y=(rand.nextInt(range));
contents[x] [y] = "*";
}
}

public String [][] getarray() {
return contents;
}

// use this function to copy the array

public void fillarray(String[][] temp) {
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
temp[i][j]=contents[i][j];
}
}
}

public static void main(String[] arguments) {
ArrayClass poop = new ArrayClass();
poop.fill();

String [] [] temp = new String [9][9];

poop.fillarray(temp); // copy array here instead

poop.contents[5][5]="7";
poop.mutate ();
for (int i=0;i<9;i++){
for (int j=0;j<9;j++) {
System.out.print(temp[i][j]);
if (j==8) {
System.out.println (" ");
}
}
}
System.out.println(" ");
for (int i=0;i<9;i++){
for (int j=0;j<9;j++) {
System.out.print(poop.contents[i][j]);
if (j==8) {
System.out.println (" ");
}
}
}
}
}




First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites
Thanks for that guys.
I was trying to be clever and return the array into temp but it''s obviously not as simple as that :-)

Share this post


Link to post
Share on other sites
Thanks for that guys.
I was trying to be clever and return the array into temp but it''s obviously not as simple as that :-)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!