Java 返回一个 ArrayList 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19562516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:21:34  来源:igfitidea点击:

return an ArrayList method

javaarraylist

提问by Bopeng Liu

This is a drive method for two other classes. which i posted here https://codereview.stackexchange.com/questions/33148/book-program-with-arraylist

这是另外两个类的驱动方法。我在这里发布的 https://codereview.stackexchange.com/questions/33148/book-program-with-arraylist

I need some help for the private static ArrayList getAuthors(String authors) method. I am kind a beginner. so please help me finish this drive method. or give me some directions.

我需要一些关于私有静态 ArrayList getAuthors(Stringauthors) 方法的帮助。我是个初学者。所以请帮我完成这个驱动方法。或者给我一些指示。

Instruction

操作说明

some of the elements of the allAuthors array contain asterisks “*” between two authors names. The getAuthors method uses this asterisk as a delimiter between names to store them separately in the returned ArrayList of Strings.

allAuthors 数组的某些元素在两个作者姓名之间包含星号“*”。getAuthors 方法使用此星号作为名称之间的分隔符,以将它们分别存储在返回的字符串 ArrayList 中。

import java.util.ArrayList;

public class LibraryDrive {

public static void main(String[] args) {

    String[] titles = { "The Hobbit", "Acer Dumpling", "A Christmas Carol",
            "Marley and Me", "Building Java Programs",
    "Java, How to Program" };

    String[] allAuthors = { "Tolkien, J.R.", "Doofus, Robert",
            "Dickens, Charles", "Remember, SomeoneIdont",
            "Reges, Stuart*Stepp, Marty", "Deitel, Paul*Deitel, Harvery" };

    ArrayList<String> authors = new ArrayList<String>();
    ArrayList<Book> books = new ArrayList<Book>();

    for (int i = 0; i < titles.length; i++) {
        authors = getAuthors(allAuthors[i]);
        Book b = new Book(titles[i], authors);
        books.add(b);
        authors.remove(0);
    }
    Library lib = new Library(books);
    System.out.println(lib);
    lib.sort();
    System.out.println(lib);

}

private static ArrayList<String> getAuthors(String authors) {
    ArrayList books = new ArrayList<String>();
            // need help here.
    return books;
}

}

回答by Petr Mensik

Take a look at String#splitmethod, this will help you to separate authors by asterisk. This method returns an array so you will need to check how many authors are in this array and then store each of them into the ArrayList.

看看String#split方法,这将帮助您用星号分隔作者。此方法返回一个数组,因此您需要检查此数组中有多少作者,然后将每个作者存储到ArrayList.

回答by SudoRahul

Here's how you can go about doing it.

以下是您可以如何去做。

  1. Split the authorsstring you're getting as the method param based on the asterisk symbol. (Using String.split(delim)method)
  2. The resultant string[] array needs to be iterated over using a forloop and each iterated element should be added to your list. (Using List.add(elem)method)
  3. Once done, return that list(you are already doin that).
  1. authors根据星号符号拆分您获得的字符串作为方法参数。(使用String.split(delim)方法)
  2. 需要使用for循环迭代生成的 string[] 数组,并且每个迭代元素都应添加到您的列表中。(使用List.add(elem)方法)
  3. 完成后,返回该列表(您已经这样做了)。

Now that you know how to do it, you need to implement the code by yourself.

既然知道了怎么做,就需要自己实现代码了。

回答by Apostolos

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

回答by yilmazburk

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

试试这个,但实际上我不明白为什么要删除ArrayListfor 循环的零索引元素。

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

回答by Lo?c

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

我建议String.split在这里使用(但请记住,此方法使用正则表达式作为参数):

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