Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from a specific ArrayList row using with a loop?

Tags:

java

android

How do I get data from a specific ArrayList row using a loop? I've added those value into ArrayList as follow.

myArrayList.add("ID007");
myArrayList.add("PPShein");
myArrayList.add("Male");
myArrayList.add("7-Apr-1983");

I want to do something like this:

for (i=0; i < myarr.size(); i++)
{
    getName = myarr[2].value();
}

It's because of I want to display as follow.

myTextView.setText(getName); //myName : "ppshein"
like image 510
PPShein Avatar asked Jun 06 '26 03:06

PPShein


2 Answers

ArrayList has a handy method called get, which takes in an index. What you may be used to is using Arrays, such as array[3] to access the 4th element. With an ArrayList, use the get method:

for(int i = 0; i < myArr.size(); i++) {
  System.out.println(myArr.get(i)); //prints element i 
}
like image 87
CodeGuy Avatar answered Jun 07 '26 18:06

CodeGuy


You just call the row...

    String getName;
    int rowValue = 2;
    getName = myarr.get(rowValue);
like image 20
secretformula Avatar answered Jun 07 '26 16:06

secretformula