How to declare and initialize an array of integers?

74 views
Skip to first unread message

BobWirka

unread,
Feb 13, 2012, 11:59:13 AM2/13/12
to sedo...@googlegroups.com
I know this has got to be simple, but how do you declare an array of integers:

        int[]                x = {1,2,3,4};

Can't seem to figure this out...

Thanks,

Bob Wirka

Matthew

unread,
Feb 13, 2012, 12:32:11 PM2/13/12
to sedo...@googlegroups.com
Bob -

Depends where you are trying to declare that.  If inside a method, you cannot declare new arrays inside a method.

If as a class field, then you can only initialize an array like this using a define.

define int[] x = {1, 2, 3, 4}

This will make x constant though.  Otherwise, you will need to initialize the array in the started() method or some other similar initialization location.

See also: http://sedonadev.org/doc/arrays.html

matthew

Craig

unread,
Feb 13, 2012, 12:36:01 PM2/13/12
to sedo...@googlegroups.com
Hi Bob-

{ 1, 2, 3, 4 } is an "array literal", and this is only supported for define fields, where you are defining a constant:

define int[] x = { 1, 2, 3, 4 }

If you want to define an array where the contents are changeable, you need to declare an inline array:

inline int[4] x

The contents of this array will need to be defined, e.g. in your component's start() method.


Reply all
Reply to author
Forward
0 new messages