从列表通过循环获取字符串值进行显示

时间:2021-07-20 18:15:40

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.

我对如何用简单的方法来使用这个数组感到困惑。我已经将JSON中的值传递给了一个列表,现在我需要使用循环(和循环)来检索它,但我不知道如何检索。我试着读一些答案,但最后我发现自己真的很困惑。我只是想让它变得简单,就像让变量[index]变得简单,从而创建一个字符串数组、循环和获取数据一样,但我找到的所有答案都让我感到困惑。请帮助。

6 个解决方案

#1


24  

As I understand your question..

我理解你的问题。

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

从Java List类中,您必须要有方法add(E E)和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);
}

#2


7  

public static void main(String[] args) {

    List<String> ls=new ArrayList<String>();
    ls.add("1");
    ls.add("2");
    ls.add("3");
    ls.add("4");

//Then  you can use "foreache" loop to iterate.

    for(String item:ls){
        System.out.println(item);
    }

}

#3


1  

Use the For-Each loop which came with Java 1.5, and it work on Types which are iterable.

使用Java 1.5附带的For-Each循环,它可以处理可迭代的类型。

ArrayList<String> data = new ArrayList<String>();
data.add("Vivek");
data.add("Vadodara");
data.add("Engineer");
data.add("Feelance");

for (String s : data){

 System.out.prinln("Data of "+data.indexOf(s)+" "+s);

 }

#4


1  

Try following if your looking for while loop implementation.

如果您正在寻找while循环实现,请尝试遵循以下步骤。

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");

int i = 0;
while (i < myString.size()) {
    System.out.println(myString.get(i));
    i++;
}

#5


0  

List<String> al=new ArrayList<string>();
al.add("One");
al.add("Two");
al.add("Three");

for(String al1:al) //for each construct
{
System.out.println(al1);
}

O/p will be

O / p

One
Two
Three

#6


0  

Answer if you only want to use for each loop ..

如果你只想为每个循环使用答案。

for (WebElement s : options) {
    int i = options.indexOf(s);
    System.out.println(options.get(i).getText());
}

#1


24  

As I understand your question..

我理解你的问题。

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

从Java List类中,您必须要有方法add(E E)和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);
}

#2


7  

public static void main(String[] args) {

    List<String> ls=new ArrayList<String>();
    ls.add("1");
    ls.add("2");
    ls.add("3");
    ls.add("4");

//Then  you can use "foreache" loop to iterate.

    for(String item:ls){
        System.out.println(item);
    }

}

#3


1  

Use the For-Each loop which came with Java 1.5, and it work on Types which are iterable.

使用Java 1.5附带的For-Each循环,它可以处理可迭代的类型。

ArrayList<String> data = new ArrayList<String>();
data.add("Vivek");
data.add("Vadodara");
data.add("Engineer");
data.add("Feelance");

for (String s : data){

 System.out.prinln("Data of "+data.indexOf(s)+" "+s);

 }

#4


1  

Try following if your looking for while loop implementation.

如果您正在寻找while循环实现,请尝试遵循以下步骤。

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");

int i = 0;
while (i < myString.size()) {
    System.out.println(myString.get(i));
    i++;
}

#5


0  

List<String> al=new ArrayList<string>();
al.add("One");
al.add("Two");
al.add("Three");

for(String al1:al) //for each construct
{
System.out.println(al1);
}

O/p will be

O / p

One
Two
Three

#6


0  

Answer if you only want to use for each loop ..

如果你只想为每个循环使用答案。

for (WebElement s : options) {
    int i = options.indexOf(s);
    System.out.println(options.get(i).getText());
}