13 Replies - 1978 Views - Last Post: 16 August 2009 - 10:02 PM Rate Topic: -----

#1 matLab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-August 09

Make a full copy of a 2D array

Posted 16 August 2009 - 07:19 PM

How would i go about making a full 2D array copy as what I have tried below does not seem to work

private PixelColour[][] makeAFullCopy(PixelColour[][] currentPixels2D) {
		userChangedPixels2D = new PixelColour[numberOfRows][numberOfCols];
		for(int r = 0; r < numberOfRows; r++){
			for(int c = 0; c < numberOfCols; c++){
				numberOfRows = currentPixels2D.length;
				numberOfCols = currentPixels2D[0].length;
				userChangedPixels2D[r][c] = currentPixels2D[r][c];
			}
		}
		return userChangedPixels2D;
	}


This post has been edited by matLab: 16 August 2009 - 07:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Make a full copy of a 2D array

#2 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 113
  • Joined: 16-August 09

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 07:41 PM

send all the code plz
Was This Post Helpful? 0
  • +
  • -

#3 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 08:06 PM

Are you looking for a deep or shallow copy?

Quote

userChangedPixels2D = new PixelColour[numberOfRows][numberOfCols];


You need to give the userChanged one a type to start with.
Also, post any errors you are getting.

This post has been edited by syfran: 16 August 2009 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 08:11 PM

The Array class might have a method to that automatically but this will work
PixelColor[][] makeCopy(PixelColor[][] p) {
	PixelColor tmp = new PixelColor[p.length][];
	 for(i = 0; i < p.length; i++) {
		tmp[i] = new PixelColor[p[i].length];
		for(int j = 0; j < p[i].length; j++)
		   tmp[i][j] = p[i][j];
	}
	return tmp;
}


Was This Post Helpful? 1
  • +
  • -

#5 Guest_Neumann*


Reputation:

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 08:55 PM

PixelColor[][] makeCopy(PixelColor[][] p) {
   return (PixelColor[][])p.clone();
}



Edit: Try removing the cast. If your version of Java is new enough it won't need it.

This post has been edited by Neumann: 16 August 2009 - 09:42 PM

Was This Post Helpful? 1

#6 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:10 PM

View PostNeumann, on 16 Aug, 2009 - 07:55 PM, said:

PixelColor[][] makeCopy(PixelColor[][] p) {
   return (PixelColor[][])p.clone();
}


How dum I am
If PixelColor implements Clonable sure
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Neumann*


Reputation:

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:34 PM

View Postpbl, on 16 Aug, 2009 - 08:10 PM, said:

How dum I am
If PixelColor implements Clonable sure


Clone() simply creates a shallow copy of array's contents. PixelColor need not implement Clonable.
Was This Post Helpful? 0

#8 matLab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-August 09

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:38 PM

Thanks pbl and Neumann both of your ways work but I don't believe we have been told about cloning so i will stick with pbl's way.

Does pbl's way make a deep copy?

This post has been edited by matLab: 16 August 2009 - 09:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_Neumann*


Reputation:

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:42 PM

No, his way is also a shallow copy. Glad we helped though.

This post has been edited by Neumann: 16 August 2009 - 09:45 PM

Was This Post Helpful? 0

#10 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:43 PM

View PostNeumann, on 16 Aug, 2009 - 08:34 PM, said:

View Postpbl, on 16 Aug, 2009 - 08:10 PM, said:

How dum I am
If PixelColor implements Clonable sure


Clone() simply creates a shallow copy of array's contents. PixelColor need not implement Clonable.


Someone should write a tutorial on all this clone/cloneable stuff. It seems to be one of the gotchas of java. As soon as I think I understand it I realize I really don't.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Neumann*


Reputation:

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:47 PM

View Postsyfran, on 16 Aug, 2009 - 08:43 PM, said:

Someone should write a tutorial on all this clone/cloneable stuff. It seems to be one of the gotchas of java. As soon as I think I understand it I realize I really don't.


Whatever clone() is being invoked on must implement Clonable. In this case I was invoking clone() on an array. All arrays implement Clonable by default.

Clone() simply iterates through every element one-by-one and blindly copies the references to PixelColour into another array. The same exact behavior would have been done on an array of Strings, of Integers, of Booleans, etc... Because all references are practically the same. That's why PixelColour didn't have to implement Clonable... because we're not actually copying PixelColour objects, we just copy the references that point to them.

This post has been edited by Neumann: 16 August 2009 - 09:55 PM

Was This Post Helpful? 0

#12 matLab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-August 09

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:51 PM

How would you go about preforming a deep copy as I am curious to see whether or not that would work better.

EDIT: Don't worry I now think a shallow copy will work

This post has been edited by matLab: 16 August 2009 - 10:00 PM

Was This Post Helpful? 0
  • +
  • -

#13 Guest_Neumann*


Reputation:

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 09:59 PM

Performing deep copies is a lot more complex and involved. I'm pretty sure that if teacher wanted you to perform a deep copy he would've mentioned so and spent a good deal of time explaining what it actually is.

With that said, I suggest you Google something like "Java array deep copy".
Was This Post Helpful? 0

#14 matLab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-August 09

Re: Make a full copy of a 2D array

Posted 16 August 2009 - 10:02 PM

Yes Neumann I was thinking the same thing.

To satisfy my curiosity I will just google it.

This post has been edited by matLab: 16 August 2009 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1