Re: How to declare a multidimensional array?

621 views
Skip to first unread message

Sven Efftinge

unread,
May 1, 2013, 2:25:16 PM5/1/13
to xtend...@googlegroups.com
It's not supported out-of-the box. You can add support by declaring a Java class like this :

public class ArrayLiterals2 {

@Inline("new int[$1][$2]")
public static int[][] intArray(int outerSize, int innerSize) {
throw new UnsupportedOperationException();
}

}


and import it like this in Xtend :

import static extension my.lib.ArrayLiterals2.*


  val int[][] arr = intArray(10,20)
 

@Inline tells the compiler to replace any calls to the annotated method with the to-be-inlined Java expression (the 'value' template).
Note that the @Inlined is not stable API (flagged as @Beta) and will likely be removed once Xtend has support for more powerful method-macros.

On May 1, 2013, at 2:39 PM, funk...@gmail.com wrote:

What's Xtend's syntax equivalent for Java's multi-dimensional array syntax:
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?

NewArrayOfSize() is only set up for single dimensions.



--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

vangel...@gmail.com

unread,
Sep 27, 2014, 1:54:45 PM9/27/14
to xtend...@googlegroups.com
Hi,

Thanks, for the beginners' sake, could you please also give the syntax how to get and set arr[x][y]?

Cheers,
Attila

Sven Efftinge

unread,
Sep 27, 2014, 3:16:49 PM9/27/14
to xtend...@googlegroups.com
Hi Attila,

use ‘get' as in :

val int[][] arr = #[#[1,2], #[3,4]]
assertEquals(3, arr.get(1).get(0))

Sven


For more options, visit https://groups.google.com/d/optout.

vangel...@gmail.com

unread,
Sep 29, 2014, 5:33:47 PM9/29/14
to xtend...@googlegroups.com
Hi Sven,

Thanks for the quick reply. Then

arr[x][y] = n;

in Java would become:

arr.get(x).set(y, n)

in xtend. This may be slightly confusing (especially for others, who did not write the code).
Furthermore

arr[x][y]++;

in Java would become something like:

arr.get(x).set(y, arr.get(x).get(y) + 1)

This is much worse to read. What is the solution for this?
I imagine it would be possible to write some static setter or increaser functions (e.g. with @Inline), but it still does not feel right.

Regards,
Attila

Stefan Oehme

unread,
Sep 30, 2014, 3:05:32 AM9/30/14
to xtend...@googlegroups.com, vangel...@gmail.com
You are right, this is inconvenient and we are looking for ways to improve this. The workaround depends on what you are trying to do. If it's just a few places, you could write some extension methods like array.increment(i, j). If you have a single class that is very array-heavy, you could just write that one class in Java.

ramanapa...@gmail.com

unread,
Nov 29, 2018, 6:27:22 AM11/29/18
to Xtend Programming Language
What is alternate way to store and retrieve values in multidimensional array in Xtend ? 

Christian Dietrich

unread,
Nov 29, 2018, 6:30:19 AM11/29/18
to Xtend Programming Language
Hi, what is your exact problem with defining own array extensions?

Ramana Pandiri

unread,
Nov 29, 2018, 6:54:32 AM11/29/18
to xtend...@googlegroups.com
I'm unable to initialize multi dimensional array in xtend class.
I want to add some csv file elements to multi dimensional array and then I want to validate. 

Thanks & Regards, 
RAMANA. 


On Thu 29 Nov, 2018, 5:00 PM Christian Dietrich, <christian.di...@gmail.com> wrote:
Hi, what is your exact problem with defining own array extensions?

--
You received this message because you are subscribed to a topic in the Google Groups "Xtend Programming Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xtend-lang/4R9Amet8FEQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Dietrich

unread,
Nov 29, 2018, 7:01:26 AM11/29/18
to Xtend Programming Language
Xtext and Xtend 2.16 (due next week)
will add a

@Pure
    @Inline("new $4[$1][$2]")
    public static <T> T[][] newArrayOfSize(int size0, int size1) {
        throw new UnsupportedOperationException(
                "This method relies on the inlined compilation (see @Inline annotation), and cannot be used from Java or with an uncustomized interpreter."); //$NON-NLS-1$
    }

you can add something like that yourself if you need higher dimensions or have it in older versions
Reply all
Reply to author
Forward
0 new messages