1

Is it possible to create a multidimensional array in scala without using the "Array()"

Like this in java:

int[][] myIntArray = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
5
  • 1
    Why do you need to do it without Array()? Commented Sep 12, 2014 at 23:37
  • Its a bit cleaner and like it that way... (This is more like a yes / no question) Commented Sep 12, 2014 at 23:42
  • 1
    Try to provide a type alias for Array to simplify the notation. Thats what you want? Commented Sep 12, 2014 at 23:45
  • More or less yes... (Seems a bit overkill for an array of 16 Integers) Commented Sep 12, 2014 at 23:48
  • Or probably not... (See answer) Commented Sep 12, 2014 at 23:50

1 Answer 1

8

If i understood correctly, you dont want to declare the array repeating Array a lot of times.

You can try this:

val > = Array

val x: Array[Array[Int]] = >(
  >(1, 2, 3),
  >(4, 5, 6),
  >(7, 8, 9)
)

Source (There are other suggestions also)

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I wanted to do. I just couldn't find it anywhere.
Hi JosEdu, out of curiosity I'm asking this, Can't we declare the above example like this, ` val > = Array; val x: >[>[Int]] = >( >(1, 2, 3), >(4, 5, 6), >(7, 8, 9) )`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.