0

I am writing a class that has an arrayList that holds objects as a data member and for one of the methods in this class I have to convert it into a basic array. To achieve this I created a new array and used the .toArray() method. Now at the end of this method I have to put it back into the original arrayList. It appears that array doesn't have a .toArrayList(). I'm just wondering what the most simple way to convert would be. Thanks for any help.

Edit: forgot to say what the arrayList was holding (objects)

2
  • 2
    Arrays.asList? You can then use new ArrayList(Arrays.asList(...)) or anInstanceOfList.addAll(Arrays.asList(...)) depending on your needs Commented Mar 13, 2015 at 2:46
  • All sounds very inefficient. Why do you have to convert from ArrayList to an array? Commented Mar 13, 2015 at 2:49

2 Answers 2

2
new ArrayList<Element>(Arrays.asList(array))
Sign up to request clarification or add additional context in comments.

Comments

1

You don't mention the array type, but assuming it's an array of object(s) (as opposed to primitives) you could use Arrays.asList(T...) like

String[] arr = {"Hello", "World"};
List<String> al = new ArrayList<>(Arrays.asList(arr));

2 Comments

(I believe the OP wants an instance of ArrayList at the end ;))
Will this work? since asList really just returns a List view of the array?

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.