How to string array syntax?

Hi.
I need string array with 10 elments, and cycle to access each element.
I didnt found examples in wiki.

Thanx for help.

At least according to Designer’s syntax highlighting it doesn’t appear that openHAB’s rules support arrays so you will need to use a List.

val myList = newArrayList('One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten')

Looping will depend on what you want to do. You can do a forEach:

myList.forEach[ str |
    // do stuff
]

or an Iterator:

val iterator = myList.iterator
while(iterator.hasNext) {
    val str = i.next
    // do stuff
}

Needing to create an array like this and iterate over them is a rare need which is why you didn’t find an example in the wiki.

Rich

2 Likes

How to create fixed size array?
I use this code, but it ugly:

var OledLines = newArrayList()
(0..31).forEach[
    OledLines.add("")
]

 OledLines.set(20, "Test")

I use it to post messages to OLED display in particular line.

The Rule’s DSL doesn’t have good support for arrays. ArrayList isn’t really an array, its a collection and it is not fixed sized.

You can try to see if one the following with work:

var OledLines = newArrayList(32)

var List<String> OledLines = new ArrayList(32)

Note that you will have to import both java.util.List and java.util.ArrayList for that second example to work.

Another cheezy hack could be:

var OledLines = " , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,".split(',')