Extend/prolong an array

Dear all,is there any way to prolong an array of size N to become of size M>N by copying the last element?
Best RegardsEdmondo

Re: Extend/prolong an array

No. You have to copy it. 
In C (where there isn't a VM) an array can be thought of as a contiguous and mutable block of memory. The same idea carries over to Java. (I don't know if the array is actually contiguous on the machine, as I'm not a JVM expert, but I would imagine that this is made the case as often as possible.) This sort of thing matters from a performance perspective (data locality, because "random access" memory isn't due to caching).

If you want an extensible collection like Java's ArrayList, look into another data structure, like ArrayBuffer. At least in C, you can't have the benefit that arrays provide of contiguity *and* O(1) append/prepend. You have to pick. 

-Mike

On Thu, Jan 19, 2012 at 6:27 AM, Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com> wrote:
Dear all,is there any way to prolong an array of size N to become of size M>N by copying the last element?
Best RegardsEdmondo

Re: Extend/prolong an array

My question was the most stupid ever. I asked it in such a way that it looked complicated
val a = Array(1,2,3,4)val aextended= a ++ Array.fill(100)(a.tail.head)
Best Regards


2012/1/19 Michael Church <mike [at] knewton [dot] com>
No. You have to copy it. 
In C (where there isn't a VM) an array can be thought of as a contiguous and mutable block of memory. The same idea carries over to Java. (I don't know if the array is actually contiguous on the machine, as I'm not a JVM expert, but I would imagine that this is made the case as often as possible.) This sort of thing matters from a performance perspective (data locality, because "random access" memory isn't due to caching).

If you want an extensible collection like Java's ArrayList, look into another data structure, like ArrayBuffer. At least in C, you can't have the benefit that arrays provide of contiguity *and* O(1) append/prepend. You have to pick. 

-Mike

On Thu, Jan 19, 2012 at 6:27 AM, Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com> wrote:
Dear all,is there any way to prolong an array of size N to become of size M>N by copying the last element?
Best RegardsEdmondo


Re: Extend/prolong an array

The padTo method might be what you want (even shorter):

  a.padTo(10,a.last)  // Returns Array(1,2,3,4,4,4,4,4,4,4)

  --Rex

On Thu, Jan 19, 2012 at 10:36 AM, Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com> wrote:
My question was the most stupid ever. I asked it in such a way that it looked complicated
val a = Array(1,2,3,4)val aextended= a ++ Array.fill(100)(a.tail.head)
Best Regards


2012/1/19 Michael Church <mike [at] knewton [dot] com>
No. You have to copy it. 
In C (where there isn't a VM) an array can be thought of as a contiguous and mutable block of memory. The same idea carries over to Java. (I don't know if the array is actually contiguous on the machine, as I'm not a JVM expert, but I would imagine that this is made the case as often as possible.) This sort of thing matters from a performance perspective (data locality, because "random access" memory isn't due to caching).

If you want an extensible collection like Java's ArrayList, look into another data structure, like ArrayBuffer. At least in C, you can't have the benefit that arrays provide of contiguity *and* O(1) append/prepend. You have to pick. 

-Mike

On Thu, Jan 19, 2012 at 6:27 AM, Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com> wrote:
Dear all,is there any way to prolong an array of size N to become of size M>N by copying the last element?
Best RegardsEdmondo



Re: Extend/prolong an array

the VM cannot change the size of arrays

-------- Original-Nachricht --------
> Datum: Thu, 19 Jan 2012 12:27:23 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Extend/prolong an array

> Dear all,
> is there any way to prolong an array of size N to become of size M>N by
> copying the last element?
>
> Best Regards
> Edmondo

Copyright © 2013 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland