List manipulation in painless script

I want to create a list as A = [[0, 1, 2], [1, 2, 3], [2, 3, 4]].
How can I create it as list/array in painless?
Do I access a specific element as A[i][j]?

Like this:

def a = [[4, 2], [3, 1], [6]];
return a[1][1];

That returns 1

1 Like

Thank you. If I have to create a list of 0's with (m, n) dimensions, what should I do?

So, you probably want an array of arrays rather than a list. Here is how you do it for an array of arrays:

def a = new int[10][11]

There are two ways to do it for lists, both examples that are valid java and valid painless:

def a = IntStream.range(0, 10).mapToObj(i -> IntStream.range(0, 11).mapToObj(j -> 0).collect(Collectors.toList())).collect(Collectors.toList())

And:

def a = [];
for (int i = 0; i < 10; i++) {
  def b = [];
  for (int j = 0; j < 11; j++) {
    b.add(0);
  }
  a.add(b);
}
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.