更新相互包含的ArrayLists?

huangapple 未分类评论46阅读模式
英文:

Updating ArrayLists that contains each other?

问题

抱歉,如果有类似的问题,我真的不确定如何称呼这个概念以搜索问题。

所以我需要创建一个包含以下3个类的数据库:

public class Actor {
    private String name;
    private ArrayList<Movie> movies; //演员参演的所有电影
}

public class Movie {
    private String name;
    private ArrayList<Actor> actors;
}

public class MovieDatabase {
    private ArrayList<Movie> movieList;
    private ArrayList<Actor> actorList; //电影中的所有演员
}

我必须创建一个方法来向数据库中添加电影和演员。
最终的目标是新电影必须属于电影类,并包含其中的所有演员,新演员也是一样。

我无法理解的是,由于电影类包含一个演员对象的数组,而演员类又包含一个电影列表的数组,如何更新,以使得最终添加的新电影包含一个完整的演员列表,其中每个列表中的演员的电影列表都更新为新的电影对象?

在这种情况下,递归是适用的正确概念吗?

英文:

Apologies if there have been similar questions, I'm honestly not sure how to call this concept to search for questions.

So I need to create a database with 3 classes like below:

public class Actor {
    private String name;
    private ArrayList&lt;Movie&gt; movies; //all movies the actor has been in
}

public class Movie {
    private String name;
    private ArrayList&lt;Actor&gt; actors;
}

public class MovieDatabase {
    private ArrayList&lt;Movie&gt; movieList;
    private ArrayList&lt;Actor&gt; actorList; //all actors in the movie
}

I have to create a method to add a movie and an actor to the database.
The final goals is that the new movie needs to be of the Movie class, and contains all the actors that are in it, same for the new actor.

What I cannot figure out is that, since the Movie class contains an array of Actor objects, and the Actor class contains an array of Movie list, how do you update so that in the end, the new Movie added contains a complete list of Actors in it, with each Actor in the list having their movie lists updated with the new Movie object?

Is recursion the right concept to apply in this case?

答案1

得分: 0

假设添加了一部新电影

Movie movie = new Movies();

movie.setActors(listOfActors)

现在对于每个演员,您需要更新电影列表

listOfActors.forEach(actor -&gt; addMovieToActor(movie,actor));


public addMovieToActor(Movie movie,Actor actor){

   List&lt;Movies&gt; existingMovies =actor.getMovies();

   existingMovies.add(movie);
}

根据您的需求,您可能需要在更新之间进行同步处理。

英文:

Suppose a new Movie gets added

Movie movie = new Movies();

movie.setActors(listOfActors)

Now for each actor you need to update the movie list

listOfActors.forEach(actor -&gt; addMovieToActor(movie,actor));


public addMovieToActor(Movie movie,Actor actor){

   List&lt;Movies&gt; existingMovies =actor.getMovies();

   existingMovies.add(movie);
}

Depending on your needs, you may need to take care of synchronization between updates.

答案2

得分: 0

我认为在这里不适合使用递归,尽管你可以使用它。这些数据库存在循环依赖,所以你只需要在更新后进行同步。换句话说,在添加新条目之后,请确保两个数据库都更新了缺失的信息。可以通过将ArrayList替换为HashMap来更轻松地进行同步,你可能需要重构你的类来实现这一点。我在简化时省略了列表,你可以将它们作为参数添加进去:

void update(String movieName, String actorName) {
    if (movies.get(movieName) == null) {
        movies.put(movieName);
    } 
    if (actors.get(actorName) == null) {
        actors.put(actorName);
    }
}

使用方式如下:

HashMap<String> actors = new HashMap<>();
HashMap<String> movies = new HashMap<>();
actors.put("foo");
movies.put("bar");
// 更新完成后,与刚刚添加的内容进行同步
update("foo", "bar");
英文:

I don't think recursion is appropriate here, although you could use it. The databases have a circular dependency so you just need to synchronize after updates. In other words, make sure that after you add a new entry, both databases are updated with the missing information. Synchronization can be made easier by swapping ArrayList out for a HashMap, you may need to refactor your classes to do this. I left out the lists for simplicity, you can add them in as a parameter:

void update(String movieName, String actorName) {
    if (movies.get(movieName) == null) {
        movies.put(movieName);
    } 
    if (actors.get(actorName) == null) {
        actors.put(actorName);
    }
}

Use as:

HashMap&lt;String&gt; actors = new HashMap&lt;&gt;();
HashMap&lt;String&gt; movies = new HashMap&lt;&gt;();
actors.put(&quot;foo&quot;);
movies.put(&quot;bar&quot;);
// Update happened, synchronize with what you just added
update(&quot;foo&quot;, &quot;bar&quot;);

huangapple
  • 本文由 发表于 2020年6月29日 16:58:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/62634607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定