We are about to switch to a new forum software. Until then we have removed the registration on this forum.
FYI this is java
I can't find anything online so I was hoping you guys could help me out. So, i have an arraylist of values (ex: 3,2,5,5,2) and a temp arraylist with the same value (3,2,5,5,2) however, this temp array is not referencing the same thing as arraylist. So, if i do collections.sort(temp arraylist), it then get (2,2,3,5,5). I know want to delete the 5's. so if the arraylist has a 5 in it, it's removed. so now, arraylist is (3,2,2). Now, i want temp arraylist to get the same values from arraylist, but i want to sort temp arraylist and leave arraylist alone. How can i do that? since the only way that i know to make two arraylists similar is temp arraylist = arraylist, but then they reference the same thing so when i sort temp arraylist, arraylist is also sorted. Any ideas? thanks.
Answers
If you have two array lists a1 and a2 then to make a1 hold the same vales as a2 then
If I have got the right syntax
Okay, so just to double check, of I sort a1, a1 will be sorted while a2 will not.
For ArrayList, we can use its method clone() in order to get a new 1 w/ same content:
http://docs.Oracle.com/javase/8/docs/api/java/util/ArrayList.html#clone--
Then we can use Collections.sort() or any other mutable methods on it w/o modifying the original : :D
http://docs.Oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-
Given that you're storing whole numerical values in your ArrayList<Integer> container; how about IntList instead? ;;) https://Processing.org/reference/IntList.html
We can use its values() or array() methods in order to get an
int[]
array.Then pass it as argument for another
new
IntList as a means to attain the same result as ArrayList's clone(): O:-)BtW, IntList got its own sort() & sortReverse() methods builtin: \m/
Cool, thanks. I never learned int list (my high school isn't big in programming, so the hardest thing we've probably coded is an arraylist)
@dwang040, just now realized that ArrayList got an overloaded constructor which accepts any Collection as its argument: 3:-O
http://docs.Oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList-java.util.Collection-
Therefore now, rather than relying on its clone() method:
sortedNums = (ArrayList<Integer>) nums.clone();
We can simply instantiate another ArrayList passing the original Collection to it:
sortedNums = new ArrayList<Integer>(nums);
Although we can shallow-clone any Collection by creating another 1 and passing the original 1 as its argument; if the variable which is tasked to hold the cloned Collection happens to be
final
, we can't assign the newly-created clone to it! :-SSIn this case, we're obliged to rely on clear() + addAll() as @quark advised: L-)
I advocate a lot about declaring variables w/
final
as much as possible.Recycling an object by re-using it rather than creating another 1 is much more "ecological": O:-)
Okay, thanks for the update!
1 more final update. This time for how to do the same for
final
IntList: :PGreat! Thanks for the help!