Thursday, August 25, 2011

Manipulate array in obpm

for example
param as Any[]
param[] = “A”
param[] = “B”

If we convert it to java code, it should be something like this
Object[] param = new Object[]{“A”, “B”};
The "PBL Java Style" syntax is mostly like Java, but it is not 100% equivalent. There are no fixed length arrays. All arrays are dynamic (like Java ArrayList). You declare an array by doing;String[] test; You append elements by using "[]=" operator.
test[] = "Hello";
test[] = "World";
You access elements like normal arrays
You remove elements with the "delete()" method.
You can also initialize an array with an array literal expression
test = { "Hello", "World", "again" };

Manipulating Arrays

Change Elements
To change an array element, you can just access it with its list number and assign it a new value:
	products = ["A", "B", "C"]
	products[2] = "D"
This changes the value of the element with the list number two (remember, arrays start counting at zero, so the element with the list number two is actually the third element.) As we changed "C" to "D", the array now contains "A", "B" and "D".
Add Elements
Now, suppose that we want to add a new element to the array. We can add a new element in the last position by just assigning a value to the next position. If it does not exist, it is added at the end:
	products = ["A", "B", "C"]
	products[3] = "D"
Now, the array has four elements: "A", "B", "C" and "D".
It is not necessary to know the array length to add an element at the end. In order to do it, the add ([]) operator or the extend method can be used.
Example, the following method is equivalent to the previous one:
	products = ["A", "B", "C"]
	extend products      // add at the end
	    using "D"
Delete Elements
Elements can be deleted from an array by using the delete operator:
	products = ["A", "B", "C"]
	delete products[0]   // delete first element
Now, the array has two elements "B", "C".
Find Elements
The 'in' operator can be used to check if an element is contained in an array. The following code checks whether "A" is contained in the array products:
	products = ["A", "B", "C"]
	if "A" in products then
	    display "'products' contains the element 'A'"
Now, if you want to get the index of the first occurrence of an element:
	products = ["A", "B", "A", "C"]
	index = indexOf(products, "A")
	if index != -1 then
	    display "'A' is located at position : " + index 
Last examples will show the index 0. Instead, if you want to find the last occurrence, 'lastIndexOf' can be used:
	products = ["A", "B", "A", "C"]
	index = lastIndexOf(products, "A")
	if index != -1 then
	    display "'A' is located at position : " + index 

No comments:

Post a Comment