two arraylists, same value but not the same reference

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

    a1.clear();
    a1.addAll(a2);
    

    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.

  • edited May 2016 Answer ✓

    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-

    // forum.Processing.org/two/discussion/16803/
    // two-arraylists-same-value-but-not-the-same-reference
    
    // GoToLoop (2016-May24)
    
    final int QTY = 10, RANGE = 100;
    
    ArrayList<Integer> nums = new ArrayList<Integer>(QTY);
    for (int i = 0; i++ < QTY; nums.add((int) random(RANGE)));
    
    ArrayList<Integer> sortedNums = (ArrayList<Integer>) nums.clone();
    java.util.Collections.sort(sortedNums);
    
    println("original:", nums);
    println("cloned:  ", sortedNums);
    
    exit();
    
  • edited May 2016

    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/

    1. https://Processing.org/reference/IntList_sort_.html
    2. https://Processing.org/reference/IntList_sortReverse_.html

    // forum.Processing.org/two/discussion/16803/
    // two-arraylists-same-value-but-not-the-same-reference
    
    // GoToLoop (2016-May24)
    
    final int QTY = 10, RANGE = 100;
    
    IntList nums = new IntList(QTY);
    for (int i = 0; i++ < QTY; nums.append((int) random(RANGE)));
    
    IntList sortedNums = new IntList(nums.values());
    sortedNums.sort();
    
    println("original:", nums);
    println("cloned:  ", sortedNums);
    
    exit();
    
  • 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)

  • edited May 2016

    @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);

    // forum.Processing.org/two/discussion/16803/
    // two-arraylists-same-value-but-not-the-same-reference
    
    // GoToLoop (2016-May24)
    
    import java.util.Collections;
    import java.util.List;
    
    final int QTY = 10, RANGE = 100;
    
    List<Integer> nums = new ArrayList<Integer>(QTY);
    for (int i = 0; i++ < QTY; nums.add((int) random(RANGE)));
    
    List<Integer> sortedNums = new ArrayList<Integer>(nums);
    Collections.sort(sortedNums);
    
    println("original:", nums);
    println("cloned:  ", sortedNums);
    
    exit();
    
  • edited May 2016

    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! :-SS

    import java.util.List;
    
    // Both nums & sortedNums variables are sealed against
    // later reassignments via final keyword:
    final int QTY = 10, RANGE = 100;
    final List<Integer> nums = new ArrayList<Integer>(QTY);
    final List<Integer> sortedNums = new ArrayList<Integer>(QTY);
    
    // Compilation Error: sortedNums is final!
    sortedNums = new ArrayList<Integer>(nums);
    

    In this case, we're obliged to rely on clear() + addAll() as @quark advised: L-)

    1. http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#clear--
    2. http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#addAll-java.util.Collection-

    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:-)

    // forum.Processing.org/two/discussion/16803/
    // two-arraylists-same-value-but-not-the-same-reference
    
    // GoToLoop (2016-May24)
    
    import java.util.Collections;
    import java.util.List;
    
    // Both nums & sortedNums variables are sealed via final keyword:
    final int QTY = 10, RANGE = 100;
    final List<Integer> nums = new ArrayList<Integer>(QTY);
    final List<Integer> sortedNums = new ArrayList<Integer>(QTY);
    
    for (int i = 0; i++ < QTY; nums.add((int) random(RANGE)));
    
    // Compilation Error: sortedNums is final!
    //sortedNums = new ArrayList<Integer>(nums);
    
    sortedNums.clear(); // Assuming sortedNums is perhaps already holding elements.
    sortedNums.addAll(nums);
    Collections.sort(sortedNums);
    
    println("original:", nums);
    println("cloned:  ", sortedNums);
    
    exit();
    
  • Okay, thanks for the update!

  • edited May 2016 Answer ✓

    1 more final update. This time for how to do the same for final IntList: :P

    // forum.Processing.org/two/discussion/16803/
    // two-arraylists-same-value-but-not-the-same-reference
    
    // GoToLoop (2016-May24)
    
    // Both nums & sortedNums variables are sealed via final keyword:
    final int QTY = 10, RANGE = 100;
    final IntList nums = new IntList(QTY), sortedNums = new IntList(QTY);
    
    for (int i = 0; i++ < QTY; nums.append((int) random(RANGE)));
    
    // Compilation Error: sortedNums is final!
    //sortedNums = new IntList(nums.values());
    
    sortedNums.clear(); // Assuming sortedNums is perhaps already holding elements.
    sortedNums.append(nums);
    sortedNums.sort();
    
    println("original:", nums);
    println("cloned:  ", sortedNums);
    
    exit();
    
  • Great! Thanks for the help!

Sign In or Register to comment.