Help with Java code-example included

Started by
3 comments, last by Hawtrey 21 years, 3 months ago
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[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[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… </i>
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.
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
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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 :-)

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 :-)

This topic is closed to new replies.

Advertisement