3

I'm translating a Java program into X10 and have run into a couple problems that I was wondering if anyone could help me translate.

Here's one Java segment that 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 here's what I've done 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]();
}

The line that's generating a mess of error statements is this:

list(k)=new ArrayList[Posting]();

Any suggestions and maybe an explanation on what I'm doing wrong?

Lii
  • 11,553
  • 8
  • 64
  • 88
Matt Nowak
  • 31
  • 3
  • What makes you think you're doing something wrong? Is there a compiler error or runtime error message you can list? – maerics Mar 27 '12 at 05:15
  • 1
    I don't know if you noticed this, but `ArrayList[] list = new ArrayList[this.V];` is an **array of `ArrayList`s** (terrible Java code btw). I don't know x10, but after browsing a bit online it doesn't look like you translated it quite right... – trutheality Mar 27 '12 at 05:32

2 Answers2

1

Agreed with trutheality. You need to define list as something like Rail[ArrayList[Posting]] :

var list:Rail[ArrayList[Posting]]=new Rail[ArrayList[Posting]](this.V);

Also, as X10 supports type inference for immutable variables, it's often better to use val instead of var and omit the type declaration altogether:

val list = new Rail[ArrayList[Posting]](this.V);
Josh Milthorpe
  • 956
  • 1
  • 14
  • 27
1

Here is 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 a single statement to create an initialized array.