Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the string value from List<String> through loop for display

Tags:

java

android

I'm confused about on how to use this array in a way that is simple. Well I already passed the values from the JSON into a List and now I need to retrieve it using a loop (and just loop) but I don't know how. Tried reading some answers but I found myself really confused in the end. I just want it to be simple as making a String array, loop and fetch data by getting the variable[index] simple as that but all the answers I've found just lead me into confusion. Help please.

like image 974
KaHeL Avatar asked Jul 16 '12 10:07

KaHeL


People also ask

How do you find the value in a list?

List get() method in Java with Examples. The get() method of List interface in Java is used to get the element present in this list at a given specific index. Syntax : E get(int index) Where, E is the type of element maintained by this List container.

Is string iterable in Java?

Many Java framework classes implement Iterable , however String does not. It makes sense to iterate over characters in a String , just as one can iterate over items in a regular array.


1 Answers

As I understand your question..

From Java List class you have to methods add(E e) and get(int position).

add(E e)

Appends the specified element to the end of this list (optional operation).

get(int index)

Returns the element at the specified position in this list.

Example:

List<String> myString = new ArrayList<String>();

// How you add your data in string list
myString.add("Test 1");
myString.add("Test 2");
myString.add("Test 3");
myString.add("Test 4");

// retrieving data from string list array in for loop
for (int i=0;i < myString.size();i++)
{
  Log.i("Value of element "+i,myString.get(i));
}

But efficient way to iterate thru loop

for (String value : myString)
{
  Log.i("Value of element ",value);
}
like image 156
user370305 Avatar answered Oct 20 '22 00:10

user370305