Asked 3 years ago
15 Sep 2021
Views 1078
Beatrice

Beatrice posted

How to loop through ArrayList and get the value of each element in Android ?


    public ArrayList<String> ids = new ArrayList<String>();


I have ArrayList of the String, I set a value on the textchanged of the EditText and want to get all values from the ArrayList at the Final button call
How to loop through ArrayList and get the value of each element in Android ?
web-api

web-api
answered Sep 15 '21 00:00


1. use for loop to iterate through ArrayList and get the value of each element

for(int i=0; i<ids.size(); i++) {
                             String id=ids.get(i);
 }


2. Use Iterator interface to iterate through ArrayList and get the value of each element


   Iterator<String> i=ids.iterator();
   while(i.hasNext()){
        String s =i.next();
   }


in above code ,hasNext() method returns true if the iteration has more elements. so it keep looping until Iterator have element.
next() method returns the next element in the iteration. at the start, it will return the first element and so on.
Post Answer