0

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 (??????????)

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

"functie" use a string argument. i call the function s.functie(x); where x is an URL
@Bogdan S: Ok, use it with a url then! :)
My function gets a url as parameter and return a list of two String Array (first contain a String array of links, and second a string array of description). In main() i need to access only the first element of the list and print it. Can you please be more specific. Thank you
I think you mean s.functie("...").get(0);
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.