www.digitalmars.com         C & C++   DMDScript  

D - arraycopy in Java --> this in D?

reply Brad Anderson <brad sankaty.dot.com> writes:
Java:
-------------
System.arraycopy (logFonts, 0, newLogFonts, 0, nFonts);



D:
-------------
newLogFonts = logFonts.dup;  // do I need the .dup?

or

newLogFonts[] = logFonts[];


TIA,
BA
Feb 15 2004
next sibling parent Vathix <vathix dprogramming.com> writes:
 newLogFonts = logFonts.dup;  // do I need the .dup?
 
 or
 
 newLogFonts[] = logFonts[];
 
 
 TIA,
 BA

I don't know Java but I can explain what the difference is. dup creates a new array. A slice (including []) on the left side of an assignment copies into the existing buffer.
Feb 15 2004
prev sibling next sibling parent John Reimer <jjreimer telus.net> writes:
Brad Anderson wrote:

 Java:
 -------------
 System.arraycopy (logFonts, 0, newLogFonts, 0, nFonts);
 
 
 
 D:
 -------------
 newLogFonts = logFonts.dup;  // do I need the .dup?

If these two arrays are dynamic (declared []), I think you need the "dup" or else newLogFonts will be a reference to the same array as logFonts.
 or
 
 newLogFonts[] = logFonts[];

This is the array copying form for static-sized arrays (declared fixed length - eg. int[4] a;)
Feb 15 2004
prev sibling parent "Ben Hinkle" <bhinkle4 juno.com> writes:
"Brad Anderson" <brad sankaty.dot.com> wrote in message
news:c0p5ee$31g2$1 digitaldaemon.com...
| Java:
| -------------
| System.arraycopy (logFonts, 0, newLogFonts, 0, nFonts);
|
|
|
| D:
| -------------
| newLogFonts = logFonts.dup;  // do I need the .dup?

Throws away any existing newLogFonts and replaces it with a newly allocated
copy of logFonts.

| or
|
| newLogFonts[] = logFonts[];

This would work only if newLogFonts had the same length as logFonts.
For the same semantics as the Java statement you want
 newLogFonts[0..nFonts] = logFonts[0..nFonts];
This will not allocate any memory. If the arrays don't have enough space the
array-bounds checker
will error.

|
|
| TIA,
| BA
Feb 15 2004