I have function returning an ArrayList<String[]>
. How can I get values from this returning ArrayList<String[]>
?
Here is an example of using a "for-each loop" to iterate through String elements in an ArrayList.
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? 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<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
.
If you're interested in the String[]
objects, then I suggest using the enhanced for loop:
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:
private void doSomethingWith(String[] strings) {
for (String string : strings) {
if (string != null) {
doSomethingWith(string);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.