java – return an ArrayList method

java – return an ArrayList method

try this

private static ArrayList<String> getAuthors(String authors) {
    ArrayList books = new ArrayList<String>();
      String[] splitStr = authors.split(\*);
      for (int i=0;i<splitStr.length;i++) {
        books.add(splitStr[i]);
       }
    return books;
}

What I would suggest is to use String.split like here (but keep in mind that this method uses a regex as parameter):

private static ArrayList<String> getAuthors(String authors) {
    ArrayList books = new ArrayList<String>();
    String[] strgArray = authors.split(\*); 
    books.addAll(Arrays.asList(strgArray));
    return books;
}

or

private static ArrayList<String> getAuthors(String authors) {
    String[] strgArray = authors.split(\*); 
    ArrayList books = Arrays.asList(strgArray);
    return books;
}

java – return an ArrayList method

Try this one but actually i do not understant why you remove zero indexed element of ArrayList in for loop.

private static ArrayList<String> getAuthors(String authors) {
    ArrayList<String> array = new ArrayList<String>();
    String[] authorsArray = authors.split(\*);
    for(String names : authorsArray );
        array.add(names);
    return array;
}

Leave a Reply

Your email address will not be published.