简体   繁体   English

获取返回ArrayList的函数的值<String[]>

[英]get values of a function returning ArrayList<String[]>

I have function returning an ArrayList<String[]> . 我有返回ArrayList<String[]>函数。 How can I get values from this returning ArrayList<String[]> ? 我如何从返回的ArrayList<String[]>获取值?

Here is an example of using a "for-each loop" to iterate through String elements in an ArrayList. 这是使用“ for-each循环”迭代ArrayList中的String元素的示例。

    ArrayList<String> list = new ArrayList<String>();
    ...
    // For every item in the list
    for(String value: list) {
        // print the value
        System.out.println(value);
    }

What is a "for-each" loop? 什么是“ for-each”循环? http://download.oracle.com/javase/1.5.0/docs/guide/language/foreach.html http://download.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

Also remember you can randomly access values in the ArrayList using the index. 还请记住,您可以使用索引随机访问ArrayList中的值。

ArrayList<String> list = new ArrayList<String>();
list.add("0");
list.add("1");

int index = 1;
list.get(index); // You get the value 1
foreach(object o in arrayList)
{
// cast to appropriate type
// eg string s = o as string;
// ...
}
ArrayList<String[]> list = new ArrayList<String[]>();

for(int i=0; i<list.size(); i++){
    String[] stringArray = list.get(i);

    for(String s : stringArray) {
       System.out.println(s);
    }

    or 

    for(int j=0; j<stringArray.length; j++) {
       System.out.println(stringArray[j]);
    }

}
//If just print out
ArrayList<String[]> list = new ArrayList<String[]>();
...
for(String[] item : list) {
    //Use Arrays.toString
    System.out.println(Arrays.toString(item));
}

Each list entry is either an array of Strings or null . 每个列表条目都是String数组或null

If you're interested in the String[] objects, then I suggest using the enhanced for loop: 如果您对String[]对象感兴趣,那么我建议使用增强的for循环:

ArrayList<String[]> result = myMethodProvidingTheList();
for(String[] strings : result) {
  if (strings != null {
    doSomethingWith(strings);
  }
}

If you need the values from the arrays now, use the same enhanced for loop for the array: 如果现在需要数组中的值,请对数组使用相同的for循环:

private void doSomethingWith(String[] strings) {
  for (String string : strings) {
    if (string != null) {
       doSomethingWith(string);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM