0

I have an object array like this

Movie[] inMovies = new Movie[]{
                MovieCreator.build(filList.get(0)),
                MovieCreator.build(filList.get(1)),
                MovieCreator.build(filList.get(2))};

Now How do I dynamically add to inMovies all the elements of Arraylist filList.

I tried this but not working

List<Movie> movi = new ArrayList<Movie>();
for (String path : filList) {
movi.add(MovieCreator.build(path));
}
Movie[] inMovies = movi.toArray();
Ron Davis
  • 346
  • 8
  • 28

3 Answers3

3

Do not use arrays. Use List

List<Movie> inMovies = new ArrayList<>();
inMovies.add(...);
igo
  • 6,359
  • 6
  • 42
  • 51
1

what do you mean with dynamically?

if you just want an array out of a list use:

Movies[] inMovies = filList.toArray(new Movies[filList.size()]);

(... just saw what u tried ...)

You have to specify the type of the Array to which you want to convert the list. For that you have to set the type of array as parameter in the toArray(T[] x) method ... like shown above

0

My solution

 List<Movie> inMovies = new ArrayList<Movie>();
   for (String path : filList) {
   inMovies.add(MovieCreator.build(path));
   }
Ron Davis
  • 346
  • 8
  • 28