Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMatthew Francis
6,967 PointsDifference between Array.asList(...) and a normal array? they both return a fixed size of arrays
From my understanding is that both of them return a fixed size of Arrays, meaning that you cannot add or remove them. So what is the difference between the two? At first I thought Array.asList has a set() method and that was the main difference, but I guess I was wrong.
1 Answer
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 PointsList<String> myList = Arrays.asList("me","them","him","her", "you");
Arrays.asList() method returns a non-resizable List backed by the standard array and the .add() and .remove () methods do not work. UnsupportedOperationException is thrown by add, remove etc. as an indication that the code is trying to modify a non-resizable or unmodifiable collection.
In order to use a resizable List use the following code :
List<String> myList = new ArrayList<>(Arrays.asList("me","them","him","her", "you"));
ArrayList is implemented as a resizable array and you can add, get, set and remove elements. As more elements are added to ArrayList, its size is increased dynamically. However the normal String array is immutable and its size cannot be increased by adding elements or its elements removed.
Cheers
Sean M
7,344 PointsI didn't know this. So for Arrays.asList, you can't add or remove? but for ArrayList, you can use the add and remove function?
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 PointsYes Sean, you can easily test this on JShell REPL and an exception will be thrown when you try add/remove operations on an immutable array. I wish there was a way I could post a snapshot of my REPL testing these 2 methods for you to see.
Sean M
7,344 PointsThat's definitely good to know. Thanks for the info!
mijurg
16,044 Pointsmijurg
16,044 PointsFrom my understanding, simple arrays are a low level data structure in java. Collections framework uses all those low level data structures and other lower level code. (low level is something considered being "closer to hardware" , or you can imagine something closer to machine code, usually this code is more verbose, longer, harder to make functionality , but some very advanced programmers use it so they can write their own algorithms and for example their own collection framework , different one.) So, difference between Array.asList and normal array is that Array.asList is a method that collection framework gives us , among other low level things it implements. You can use collections framework functionality to create a low level array structures and many other things, also you can revert that low level structure you created, back to higher level code as List.. But, if you use collection framework, a lot of job is already done for you. You have documentation, algorithms, methods and consistent code base, because all java programmers use this collection to make their work easier. My suggestion is do not think too much about inter workings of java at such deep level until way later. I myself am just learning java, but it is what my understanding relating to your question is.