# 2D array problem

4 replies
Russ P.
Joined: 2009-01-31,
I'm trying to create a 2D array. I found some code on the archives of this list, and I made a few minor changes. I cannot get it to compile, but I do not understand what the problem is. Here is the code:

class Array2D[T](val N1: Int, val N2: Int) {

private val arr = Array[T](N1 * N2)

def apply(i: Int, j: Int) = arr(N1 * i + j)

def update(i: Int, j: Int, x: T) = { arr(N1 * i + j) = x }
}

When I try to compile it, I get this:

(fragment of geometry.scala):10: error: type arguments [T] do not conform to method apply's type parameter bounds [A <: AnyRef]
private val arr = Array[T](N1 * N2)
^
Is there anything I can do to make this work? Thanks.

Russ P.

--
http://RussP.us
tolsen77
Joined: 2008-10-08,
Re: 2D array problem
Try and replace with private val arr = new Array[T](N1 * N2)

On Wed, Mar 4, 2009 at 10:23 AM, Russ Paielli <russ [dot] paielli [at] gmail [dot] com> wrote:
I'm trying to create a 2D array. I found some code on the archives of this list, and I made a few minor changes. I cannot get it to compile, but I do not understand what the problem is. Here is the code:

class Array2D[T](val N1: Int, val N2: Int) {

private val arr = Array[T](N1 * N2)

def apply(i: Int, j: Int) = arr(N1 * i + j)

def update(i: Int, j: Int, x: T) = { arr(N1 * i + j) = x }
}

When I try to compile it, I get this:

(fragment of geometry.scala):10: error: type arguments [T] do not conform to method apply's type parameter bounds [A <: AnyRef]
private val arr = Array[T](N1 * N2)
^
Is there anything I can do to make this work? Thanks.

Russ P.

--
http://RussP.us

Florian Hars
Joined: 2008-12-18,
Re: 2D array problem

Russ Paielli schrieb:
> private val arr = Array[T](N1 * N2)

You declare it as an Array containig T, but initialize it as an
Array containing one Integer, like this:

scala> Array[String](3 * 4)
:5: error: type mismatch;
found : Int(12)
required: String
Array[String](3 * 4)
^

- Florian

Naftoli Gugenheim
Joined: 2008-12-17,
Re: 2D array problem
I think the problem is that the T of Array2D could be an AnyVal. Try class Array2D[T <: AnyRef] { ...

On Wed, Mar 4, 2009 at 4:23 AM, Russ Paielli <russ [dot] paielli [at] gmail [dot] com> wrote:
I'm trying to create a 2D array. I found some code on the archives of this list, and I made a few minor changes. I cannot get it to compile, but I do not understand what the problem is. Here is the code:

class Array2D[T](val N1: Int, val N2: Int) {

private val arr = Array[T](N1 * N2)

def apply(i: Int, j: Int) = arr(N1 * i + j)

def update(i: Int, j: Int, x: T) = { arr(N1 * i + j) = x }
}

When I try to compile it, I get this:

(fragment of geometry.scala):10: error: type arguments [T] do not conform to method apply's type parameter bounds [A <: AnyRef]
private val arr = Array[T](N1 * N2)
^
Is there anything I can do to make this work? Thanks.

Russ P.

--
http://RussP.us

Russ P.
Joined: 2009-01-31,
Re: 2D array problem
On Wed, Mar 4, 2009 at 1:39 AM, Trond Olsen <tolsen77 [at] gmail [dot] com> wrote:
Try and replace with private val arr = new Array[T](N1 * N2)

Thanks. That seems to work. I left out the "new".

A better error message might have saved me some time.

--Russ

On Wed, Mar 4, 2009 at 10:23 AM, Russ Paielli <russ [dot] paielli [at] gmail [dot] com> wrote:
I'm trying to create a 2D array. I found some code on the archives of this list, and I made a few minor changes. I cannot get it to compile, but I do not understand what the problem is. Here is the code:

class Array2D[T](val N1: Int, val N2: Int) {

private val arr = Array[T](N1 * N2)

def apply(i: Int, j: Int) = arr(N1 * i + j)

def update(i: Int, j: Int, x: T) = { arr(N1 * i + j) = x }
}

When I try to compile it, I get this:

(fragment of geometry.scala):10: error: type arguments [T] do not conform to method apply's type parameter bounds [A <: AnyRef]
private val arr = Array[T](N1 * N2)
^
Is there anything I can do to make this work? Thanks.

Russ P.

--
http://RussP.us

--
http://RussP.us