Java translation to x10
I was translating a java program to x10 and was running into a few issues that I was wondering if someone could help me translate. here's one java segment i'm trying to translate.
ArrayList<Posting>[] list = new ArrayList[this.V];
for (int k=0; k<this.V; ++k) {
list[k] = new ArrayList<Posting>();
}
and this is what i did in x10
var list:ArrayList[Posting]=new ArrayList[Posting](this.V);
for (var k:int=0; k<this.V; ++k) {
list(k)=new ArrayList[Posting]();
}
a string that creates a mess of error statements,
list(k)=new ArrayList[Posting]();
Any suggestions and possibly an explanation of what I am doing wrong?
source to share
Consistent with trutheality. You should define it list
as something like Rail[ArrayList[Posting]]
:
var list:Rail[ArrayList[Posting]]=new Rail[ArrayList[Posting]](this.V);
Additionally, since X10 supports type inference for immutable variables, it is often better to use val
instead of var
and omit the type declaration altogether:
val list = new Rail[ArrayList[Posting]](this.V);
source to share
Here's the code that should work for you:
val list = new Rail[ArrayList[Posting]](this.V);
for (k in 1..(this.V)) {
list(k)=new ArrayList[Posting]();
}
And you can also do
val list = new Rail[ArrayList[Posting]](this.V, (Long)=>new ArrayList[Temp]());
i.e. use one statement to create an initialized array.
source to share