Can't seem to access Arrays which are initialized in a constructor

0 views
Skip to first unread message

Lewis Flanagan

unread,
Dec 3, 2010, 6:20:36 PM12/3/10
to coms49...@googlegroups.com
Hey, I've run into a problem with my project. I can't seem to access Arrays which are initialized in a constructor. Here's a simple class that
illustrates the problem.

public class ArrayTest 
{
val classFieldArray: Array[Int];
val classFieldArray2 = new Array[Int] (10, 0);
def this ()
{
classFieldArray = new Array[Int] (10, 0);
}
public def isFine ()
{
val localArray = new Array[Int] (10, 0);
localArray(2) = 5;
}
public def isAlsoFine ()
{
classFieldArray2(2) = 5;
}
public def wontCompile ()
{
classFieldArray(2) = 5;
/*Cannot assign expression to array element of given type.
Expression: 5
Type: x10.lang.Int{self==5}
Array element: ArrayTest.this.classFieldArray(2)
Cause: Method apply(i0: x10.lang.Int){x10.array.Array#this.region.rank==1}[]
in x10.array.Array[x10.lang.Int]{self==ArrayTest#this.classFieldArray,
ArrayTest#this.classFieldArray.region!=null} cannot be called with arguments
(x10.lang.Int{self==2}); Call invalid; calling environment does not entail
the method guard. */
}
}

the methods isFine () and isAlsoFine, compile fine. The method wontCompile, won't compile. The error message eclipse gives is the comment below it. I download the version of x10 I'm using just before break so it should be the most recent version. Can anyone tell me why this is happening? I'd be appreciative.

~Lewis Flanagan

Yoav Zibin

unread,
Dec 3, 2010, 6:28:31 PM12/3/10
to coms49...@googlegroups.com
Instead of:
val classFieldArray: Array[Int];
Write:
val classFieldArray: Array[Int](1);
That will tell the compiler you are using 1-dimensional arrays.

Lewis Flanagan

unread,
Dec 3, 2010, 6:39:40 PM12/3/10
to coms49...@googlegroups.com
Thank you!
Reply all
Reply to author
Forward
0 new messages