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();
4
  • How about using toArray() method of ArrayList class Commented Oct 5, 2014 at 15:14
  • possible duplicate of Add an object to an Array of a custom class Commented Oct 5, 2014 at 15:29
  • What do you mean that "it's not working"? Commented Oct 5, 2014 at 15:30
  • The error is incompatible types Required: com.googlecode...movie found:java.lang.object Commented Oct 5, 2014 at 15:34

3 Answers 3

3

Do not use arrays. Use List

List<Movie> inMovies = new ArrayList<>();
inMovies.add(...);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

My solution

 List<Movie> inMovies = new ArrayList<Movie>();
   for (String path : filList) {
   inMovies.add(MovieCreator.build(path));
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.