problem with addBefore() in x10.util.ArrayList<T>

11 views
Skip to first unread message

Vincent Liao

unread,
Dec 9, 2010, 12:52:39 AM12/9/10
to coms4995pppp
Hi,

My group has been having problems using addBefore(Int i, T v) in x10.util.ArrayList<T> (http://dist.codehaus.org/x10/xdoc/2.1.0/x10/util/ArrayList.html). We want to add a new entry to index 0 of the list, shifting all existing elements up one index. However, using addBefore(0, newElement) seems to shift the elements but then set all the elements to the old index 0. Perhaps the element shifting in the code is incorrect? i.e. the following is being done:
a[1] = a[0];
a[2] = a[1];
...

I've provided a sample snippet of code and the output below.

For now, we're going to make our own addBefore method. Please advise if there is an easier way to fix this. Thanks!

-Vincent


Code:

cl: Rail[ArrayList[CharEntry]]
id: Int
index: Int
temp: CharEntry
...
public class CharEntry {

public val char:Char;
public val i:Int;
public val j:Int;

public def this(char:Char, i:Int, j:Int) {
this.char = char;
this.i = i;
this.j = j;
}

}
...
if (index < cl(id).size()) {
Console.OUT.println("index = " + index);
Console.OUT.println("temp.char = " + temp.char);
Console.OUT.println("BEFORE");
for (e in cl(id)) {
Console.OUT.print(e.char);
}
Console.OUT.println();
atomic cl(id).addBefore(index, temp);
Console.OUT.println("AFTER");
for (e in cl(id)) {
Console.OUT.print(e.char);
}
Console.OUT.println();
}



Output:

index = 0
temp.char = q
BEFORE
qqqqqnrrrrrrrrrrrrrrrrrr
sssssssssssssssssss
tttttttttttttttttttt
uuuuuuuuuuuuuuuuuuuuu
vvvvvvvvvvvvvvvvvvvvvv
wwwwwwwwwwwwwwwwwwwwwww
xxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzz
AFTER
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
Reply all
Reply to author
Forward
0 new messages