如何将一个逗号分隔的ArrayList添加到树中

时间:2023-01-06 19:38:40

I have a Movie object where the data members are a String title, int year, and ArrayList actors. I am a bit confused on how I can add this ArrayList<String> to my tree. I am reading this information from a file for example:

我有一个Movie对象,其中数据成员是String title,int year和ArrayList actors。我对如何将这个ArrayList 添加到我的树有点困惑。我正在从文件中读取此信息,例如:

Forrest Gump/1994/Tom Hanks
Star Wars/1977/Mark Hamill,Carrie Fisher,Harrison Ford

So far, I have been able to add everything else except for the ArrayList. I am thinking that I will need to also line.split the contents of the array. Also, some of the movies do not have multiple actors as shown in the example so I am not sure how to go about implementing this. I have tried a few different ways, but have ended up with an IndexOutOfBoundsException.

到目前为止,我已经能够添加除ArrayList之外的所有其他内容。我在想,我还需要line.split数组的内容。此外,一些电影没有多个演员,如示例所示,所以我不知道如何实现这一点。我尝试了几种不同的方法,但最终得到了IndexOutOfBoundsException。

Here is what I have so far:

这是我到目前为止:

try{
        Scanner read = new Scanner( new File("movies.txt") );
        do{
            ArrayList<String> actorList = new ArrayList<String>();
            String line = read.nextLine();
            String [] tokens = line.split("/");
            //I think I need to add another split for commas here.
            //actorList.add() here

            tree.add( new Movie(tokens[0], Integer.parseInt(tokens[1]), actorList ));
        }while( read.hasNext() );
        read.close();
    }catch( FileNotFoundException fnf ){
        System.out.println("File not found.");
    }

Her is my Constructor if needed:

如果需要,她是我的构造函数:

public Movie( String t, int y, ArrayList<String> a ){
    title = t;
    year = y;
    actors = a;
}

3 个解决方案

#1


Hopefully, below code should work. Split your comma separated actor list, convert that String array to a List and add this List to your ArrayList. Use Arrays.asList() as a neat implementation.

希望下面的代码可以工作。拆分逗号分隔的actor列表,将该String数组转换为List并将此List添加到ArrayList。使用Arrays.asList()作为一个简洁的实现。

try{
        Scanner read = new Scanner( new File("movies.txt") );
        do{
            ArrayList<String> actorList = new ArrayList<String>();
            String line = read.nextLine();
            String [] tokens = line.split("/");
            actorList.addAll(Arrays.asList(tokens[2].split(",")));

            tree.add( new Movie(tokens[0], Integer.parseInt(tokens[1]), actorList ));
        }while( read.hasNext() );
        read.close();
    }catch( FileNotFoundException fnf ){
        System.out.println("File not found.");
    }

#2


You can split the last token by comma and insert each one of the strings that are created into the actorList:

您可以用逗号分割最后一个标记,并将创建的每个字符串插入到actorList中:

...
String [] tokens = line.split("/");
String lastToken = tokens[tokens.length-1];
if (tokens.length == 3) {
    String[] actors = lastToken.split(",");
    for (String actor : actors) {
        actorList.add(actor);
    }
}
...

#3


Try this:

String actors = tokens[tokens.length-1];
actorList = Arrays.asList(actors.split(","));

#1


Hopefully, below code should work. Split your comma separated actor list, convert that String array to a List and add this List to your ArrayList. Use Arrays.asList() as a neat implementation.

希望下面的代码可以工作。拆分逗号分隔的actor列表,将该String数组转换为List并将此List添加到ArrayList。使用Arrays.asList()作为一个简洁的实现。

try{
        Scanner read = new Scanner( new File("movies.txt") );
        do{
            ArrayList<String> actorList = new ArrayList<String>();
            String line = read.nextLine();
            String [] tokens = line.split("/");
            actorList.addAll(Arrays.asList(tokens[2].split(",")));

            tree.add( new Movie(tokens[0], Integer.parseInt(tokens[1]), actorList ));
        }while( read.hasNext() );
        read.close();
    }catch( FileNotFoundException fnf ){
        System.out.println("File not found.");
    }

#2


You can split the last token by comma and insert each one of the strings that are created into the actorList:

您可以用逗号分割最后一个标记,并将创建的每个字符串插入到actorList中:

...
String [] tokens = line.split("/");
String lastToken = tokens[tokens.length-1];
if (tokens.length == 3) {
    String[] actors = lastToken.split(",");
    for (String actor : actors) {
        actorList.add(actor);
    }
}
...

#3


Try this:

String actors = tokens[tokens.length-1];
actorList = Arrays.asList(actors.split(","));