标签云

微信群

扫码加入我们

WeChat QR Code

I have an array that is initialized like:Element[] array = {new Element(1), new Element(2), new Element(3)};I would like to convert this array into an object of the ArrayList class.ArrayList<Element> arraylist = ???;


In Java9 -->List<String> list = List.of(“Hello”, “World”, “from”, “Java”);

2019年08月19日12分01秒

MarekM This answer is wrong, as this doesn't return an ArrayList. The Poster asked specifically for that.

2019年08月19日12分01秒

I think he didn't mine using List interface, because it is best practice. But if you want here is - new ArrayList<>(List.of(“Hello”, “World”, “from”, “Java”));

2019年08月19日12分01秒

The point is not about using the interface, the point isthat in your solution, the returned list is unmodifiable. That might be more of a problem, and a reason why he asked for an ArrayList

2019年08月19日12分01秒

Yep. And in the (most common) case where you just want a list, the new ArrayList call is unecessary as well.

2019年08月19日12分01秒

Luron - just use List<ClassName> list = Arrays.asList(array)

2019年08月19日12分01秒

Calum and Pool - as noted below in Alex Miller's answer, using Arrays.asList(array) without passing it into a new ArrayList object will fix the size of the list. One of the more common reasons to use an ArrayList is to be able to dynamically change its size, and your suggestion would prevent this.

2019年08月19日12分01秒

Arrays.asList() is a horrible function, and you should never just use its return value as is. It breaks the List template, so always use it in the form indicated here, even if it does seem redundant. Good answer.

2019年08月19日12分01秒

Adam Please study the javadoc for java.util.List. The contract for add allows them to throw an UnsupportedOperationException. docs.oracle.com/javase/7/docs/api/java/util/…Admittedly, from an object-oriented perspective it is not very nice that many times you have to know the concrete implementation in order to use a collection - this was a pragmatic design choice in order to keep the framework simple.

2019年08月19日12分01秒

What is the time complexity of both operations? I mean with and without using explicit arraylist construction.

2019年08月19日12分01秒

Arrays.asList() merely creates an ArrayList by wrapping the existing array so it is O(1).

2019年08月19日12分01秒

Wrapping in a new ArrayList() will cause all elements of the fixed size list to be iterated and added to the new ArrayList so is O(n).

2019年08月19日12分01秒

the implementation of List returned by asList() does not implement several of List's methods (like add(), remove(), clear(), etc...) which explains the UnsupportedOperationException. definitely a caveat...

2019年08月19日12分01秒

When the question asks for "an object of the ArrayList class" I think it's reasonable to assume the class it refers to is java.util.ArrayList.Arrays.asList actually returns a java.util.Arrays.ArrayList which is not an instanceof the other class. So a third caveat is that if you try to use it in a context requiring the above it will not work.

2019年08月19日12分01秒

+1 But note that the List returned by Arrays.asList is mutable in that you can still set elements - it just isn't resizable. For immutable lists without Guava you might mention Collections.unmodifiableList.

2019年08月19日12分01秒

haylem In your section For Educational Purposes: The Good ol' Manual Way, your arrayToList for Java 1.5+ is incorrect. You are instanciating lists of String, and trying to retrieve strings from the given array, instead of using the generic parameter type, T. Other than that, good answer, and +1 for being the only one including the manual way.

2019年08月19日12分01秒

In particular, List<String> a = Arrays.asList("first","second","third")

2019年08月19日12分01秒

List.of() will not return an instance of java.util.ArrayList, as requested in the original question. Therefore only the second option is a valid answer.

2019年08月19日12分01秒

That will be backed by the original input array, which is why you (probably) want to wrap it in a new ArrayList.

2019年08月19日12分01秒

Be careful with this solution. If you look, Arrays ISN'T returning a true java.util.ArrayList. It's returning an inner class that implements the required methods, but you cannot change the memebers in the list. It's merely a wrapper around an array.

2019年08月19日12分01秒

You can cast the List<Element> item to an ArrayList<Element>

2019年08月19日12分01秒

Mikezx6r: little correction: it's a fixed-size list. You can change the elements of the list (set method), you cannot change the size of the list (not add or remove elements)!

2019年08月19日12分01秒

Yes, with the caveat that it depends on what you want to do with the list. It's worth notng that if the OP simply wants to iterate through the elements, the array doesn't have to be converted at all.

2019年08月19日12分01秒

It's probably best not to be implementation-dependent, but Collectors.toList() actually returns an ArrayList.

2019年08月19日12分01秒

incorrect use of Stream.of(...); that will create a one element stream. Use Arrays.stream instead

2019年08月19日12分01秒

I don't think so, the 2 options are valid but the Arrays.stream is slightly 'better' since you can create it with fixed size, using the overload method with 'start', 'end' args. See also: stackoverflow.com/a/27888447/2619091

2019年08月19日12分01秒

I fail to see the fundamental difference between your loop at the end of the answer and the new ArrayList<T>(Arrays.asList(myArray)); part which you discourage to use. Both do quite the same and have the same complexity.

2019年08月19日12分01秒

The Collections one create a pointer at the beginning of the array. My loop create many pointers : one for each array member. So if the original array changes, my poiners are still directed toward the former values.

2019年08月19日12分01秒

new ArrayList<T>(Arrays.asList(myArray)); does the same, it copies the asList to an ArrayList...

2019年08月19日12分01秒

Note that this is not an ArrayList, as it was explicitely asked.

2019年08月19日12分01秒

As of java 8, Collectors.toList() will return an ArrayList. However this may differ in future versions on java.If you want a specific type of collection then use Collectors.toCollection() instead where you can specify which exact type of collection you would want to create.

2019年08月19日12分01秒

this solution doesn't seem to work with char array, either.

2019年08月19日12分01秒

This is less efficient than the accepted 9 year old answer.

2019年08月19日12分01秒

One of ArrayLists constructors accepts a ? extends Collection<T> argument, making the call to addAll redundant.

2019年08月19日12分01秒

Downvoted because that cast looks very dangerous. nothing specifies that the type of list that is returned is actually an ArrayList, as the javadoc states: "There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned"

2019年08月19日12分01秒

If you want to explicitely create an ArrayList, try this: ArrayList<String> list = Arrays.stream(array).collect(Collectors.toCollection(ArrayList::new));

2019年08月20日12分01秒

List<Element> elementList = Arrays.asList(array) creates a wrapper over the original array which makes original array available as List. Hence a wrapper object is created, nothing gets copied from the original array. Therefore, operations like add or remove elements are not allowed.

2019年08月19日12分01秒

Note that your "immutable collection" is not really immutable - the List returned by Arrays.asList is just a wrapper over the original array, and allows individual items to be accessed and modified via get and set. You should probably clarify that you mean "not add or remove elements" instead of "immutable", which means to not change at all.

2019年08月19日12分01秒

I respect your work. But as someone new to Java, List and rest of its collections (ArrayList, etc...) is already too much to handle. Why you want me to suffer more :-(

2019年08月19日12分01秒