# How can we define array in katalon to save values in for loop

**URL:** <https://forum.katalon.com/t/how-can-we-define-array-in-katalon-to-save-values-in-for-loop/10715>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [May 6, 2018, 6:06am UTC](https://forum.katalon.com/t/how-can-we-define-array-in-katalon-to-save-values-in-for-loop/10715 "2018-05-06T06:06:18Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 3, 2018, 1:18am UTC](https://forum.katalon.com/t/how-can-we-define-array-in-katalon-to-save-values-in-for-loop/10715/4 "2018-07-03T01:18:35Z")

</div>

You want array rather than List. OK. It is just possible.

I rewrote the Example.1 of [https://www.katalon.com/resources-center/tutorials/handle-web-tables/](https://www.katalon.com/resources-center/tutorials/handle-web-tables/) .

At first, just a rewrite the Example1. I did rewriting because the coding style of the original code in the URL looks a bit odd to me.

```auto
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
/**
 * reproducing the Example.1 in
 * https://www.katalon.com/resources-center/tutorials/handle-web-tables/
 *
 */
WebUI.openBrowser('')
WebUI.setViewPortSize(703, 347)
WebUI.navigateToUrl('http://demoaut-mimic.kazurayam.com/18107_testbed.html')
WebDriver driver = DriverFactory.getWebDriver()
// To locate table
WebElement table = driver.findElement(By.xpath('//table/tbody'))
// To locate rows of table it will capture all the rows available in the table
List<WebElement> rowsInTable = table.findElements(By.tagName('tr'))
// To calculate number of rows in the table
int rowsCount = rowsInTable.size()
WebUI.comment("rowsCount=${rowsCount}")
// Iterate over all the rows of the table
for (int r = 0; r < rowsCount; r++) {
    // To locate columns(cells) of that specific row
    List<WebElement> columnsInRow = rowsInTable.get(r).findElements(By.tagName('td'))
    // To calculate number of columns in that specific row
    int columnsCount = columnsInRow.size()
    WebUI.comment("Number of cells in row ${r} are ${columnsCount}")
    // Iterate over the cells of that specifc row
    for (int c = 0; c < columnsCount; c++) {
        // Retrieve text from each cell
        String cellText = columnsInRow.get(c).getText()
        WebUI.comment("Cell value of (${r},${c}) is '${cellText}'")
    } 
}
WebUI.closeBrowser()

```

Then a rewrite with arrays.

```auto
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
/**
 * reproducing the Example.1 in
 * https://www.katalon.com/resources-center/tutorials/handle-web-tables/
 *
 * using arrays rather than List
 */
WebUI.openBrowser('')
WebUI.setViewPortSize(703, 347)
WebUI.navigateToUrl('http://demoaut-mimic.kazurayam.com/18107_testbed.html')
WebDriver driver = DriverFactory.getWebDriver()
// To locate table
WebElement table = driver.findElement(By.xpath('//table/tbody'))
// To locate rows of table it will capture all the rows available in the table
WebElement[] rowsInTable = table.findElements(By.tagName('tr')) as WebElement[]
// To calculate number of rows in the table
int rowsCount = rowsInTable.length
WebUI.comment("rowsCount=${rowsCount}")
// Iterate over all the rows of the table
for (int r = 0; r < rowsCount; r++) {
    // To locate columns(cells) of that specific row
    WebElement[] columnsInRow = rowsInTable[r].findElements(By.tagName('td')) as WebElement[]
        // To calculate number of columns in that specific row
    int columnsCount = columnsInRow.length
    WebUI.comment("Number of cells in row ${r} are ${columnsCount}")
    // Iterate over the cells of that specifc row
    for (int c = 0; c < columnsCount; c++) {
        // Retrieve text from each cell
        String cellText = columnsInRow[c].getText()
        WebUI.comment("Cell value of (${r},${c}) is '${cellText}'")
    } 
}
WebUI.closeBrowser()

```

Difference of List and array comes up only when you want to add/delete an entry. With List, adding an entry is a breeze. With array, adding an entry is a cumbersome task. In the above code we are not adding entries so that you would find very little difference between the two. Almost all of Java/Groovy APIs prefer List to array, so that you need to learn the List anyway.

---

_[View the full topic](https://forum.katalon.com/t/how-can-we-define-array-in-katalon-to-save-values-in-for-loop/10715)._
