This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Extend/prolong an array

4 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
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
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
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

Michael Church
Joined: 2012-01-18,
User offline. Last seen 40 weeks 6 days ago.
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.porcu@gmail.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

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
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@knewton.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.porcu@gmail.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


ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
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.porcu@gmail.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@knewton.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.porcu@gmail.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



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