[java] Help with code?-example incl. in post.

Started by
3 comments, last by Hawtrey 21 years, 2 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
Thsi program is working exactly as you have programmed it to. When you call getarray() temp points to the array in poop, not to the array you created in the main method.

"... we should have such an empire for liberty as she has never surveyed since the creation ..."
Thomas Jefferson
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson
That''s what people have been telling me :-)
How do I get a local copy then of an array? arraycopy doesn''t work as this is tailored to single dimension arrays...
Any ideas?
This is not the best solution, because you could easily be getting NullPointerExceptions if you vary the size of the array, but this is how to do what you are after:


    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[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 void getArray(String[][] secondArray) {		for (int i=0;i<9;i++){			for (int j=0;j<9;j++) {				secondArray[i][j]= contents[i][j];			}		}	}	public static void main(String[] arguments) {		ArrayClass poop = new ArrayClass();		poop.fill();		String[][] temp = new String[9][9];		poop.getArray(temp);		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 (" ");			}		}	}}  




[edited by - stustill on January 21, 2003 10:51:31 AM] [/source]

[edited by - stustill on January 21, 2003 10:52:07 AM]
Thanks for your suggestions. To be honest I don''t even really need a copy of the array in what I was trying to eventually do,I just thought I''d make sure the original array was filling up and I created the temp.It just got a bit funny when it was run but it drove me insane trying to figure out a way to return the array :-)

This topic is closed to new replies.

Advertisement