tag:blogger.com,1999:blog-865923359735383241.post4415411355219653133..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: Java: Best way to store to an arbitrary index of an ArrayListUnknownnoreply@blogger.comBlogger7125tag:blogger.com,1999:blog-865923359735383241.post-88252836621522250822012-02-14T19:07:54.888-07:002012-02-14T19:07:54.888-07:00If you are looking for a sparse array (where most ...If you are looking for a sparse array (where most of the indices will be empty), a Map of some sort (probably a HashMap) will be your best bet. Any array-esque solution will be forced to reserve space for all the empty indices, which is not very space-efficient, and a HashMap is fast enough for most normal purposes. <br /><br />If you will eventually fill up the array up to some n, you will need to add nulls in a loop to get to the index you want. You can make this somewhat more efficient by giving it an initial capacity of the number of elements you will eventually want to store (this prevents the ArrayList from needing to resize itself). new ArrayList(n) will work fine. Unfortunately, there is no simple way of making it a certain size to begin with except adding things in a loop when you make it.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-55430970459461146112012-02-14T19:07:52.656-07:002012-02-14T19:07:52.656-07:00You can use TreeMap<key, value>, which is so...You can use TreeMap<key, value>, which is sorted in natural order by the value.<br /><br />Here you can keep value as the index. You can insert any value, it need not be in order. This seems the simplest solution.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-34630232178741569132012-02-14T19:07:52.067-07:002012-02-14T19:07:52.067-07:00HashMap is probably much less inefficient than you...HashMap is probably much less inefficient than you think, try it. Otherwise, I can think of no way of doing it more elegantly than looping and filling with null. If you want elegance of exposition at least, then you could always subclass ArrayList and add an expandingSet(position, value) method to hide all the looping and such, or something. Perhaps this isn't an option though? If not just have a utility method somewhere else for it, but this is not as nice imho, though it will work also with other types of list too I guess...<br /><br />Perhaps a wrapper class would be the best of both worlds, or perhaps it would just incur unnecessary overhead...Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-27620873433327389472012-02-14T19:07:50.359-07:002012-02-14T19:07:50.359-07:00If you definitely have to use the list and not a m...If you definitely have to use the list and not a map, then it's best you override the arraylist's add and set methods to first put a null in indexes before. No other better way IMOUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-58852354692430184072012-02-14T19:07:49.708-07:002012-02-14T19:07:49.708-07:00Sounds like you want a regular array:
You want r...Sounds like you want a regular array:<br /><br /><br />You want random access<br />You want to specify some large sizeUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-28141080783626687642012-02-14T19:07:49.139-07:002012-02-14T19:07:49.139-07:00You can use a Map<Integer, MyClass> instead....You can use a Map<Integer, MyClass> instead. Specifically, if you use HashMap, it will also be O(1) - though it will be slower than ArrayList.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-18511455356206458482012-02-14T19:07:48.612-07:002012-02-14T19:07:48.612-07:00I could use a HashMap and use the index as a key b...I could use a HashMap and use the index as a key but that's really inefficient.<br /><br /><br />Depends. If the indices you use are very sparse, it might be a lot better to use a Map. If the indices tend to be closely together, I think there is no better way than to fill it with nulls. Just write a utility function for it that you can use over and over instead of repeating the loop everywhere you need it, something like this:<br /><br />private void padTo(List<?> list, int size) {<br /> for (int i=list.size(); i<size; i++)<br /> list.add(null);<br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com