Reputation: 229
My function return a List of string array. how i access/print only the first string array from the list in main().
public class URLReader{
public List<String[]> functie(String x) throws Exception{
...
List<String[]> substrList = new ArrayList<String[]>();
substrList.add(tds2);
substrList.add(tds3);
return substrList;
}
public static void main(String[] args) throws Exception {
URLReader s = new URLReader();
for (??????????)
Upvotes: 0
Views: 500
Reputation: 1200
As other answers have stated already, to use the first element in a list, you would call the List.get(int) method.
someList.get(0);
In your code, in order to iterate over the String array in the first list index, you would want something that looks like:
for( String str : s.functie(arg).get(0) ) {
//Do something with the string such as...
System.out.println(str);
}
Upvotes: 0
Reputation: 8044
You can get the first element from a list like so:
final List<String[]> arrayList = new ArrayList<String[]>();
arrayList.get(0); // get first element
Or you can use a queue, which has built in methods for such tasks.
final Queue<String[]> linkedList = new LinkedList<String[]>();
linkedList.poll(); // get (and remove) first element
linkedList.peek(); // get (but do not remove) first element
Upvotes: 0
Reputation: 43504
If you want to iterate over all arrays (what you started writing in your question:
for (String[] array : s.functie("...")) {
...
}
If you only want the first one:
String[] array = array.get(0);
Upvotes: 2