-3

I've tried doing it many ways however if I make any changes to one copy, those changes are reflect in the other. I've also tried to deep copy with no results please help.

My deep copy method:

public static ArrayList<Integer> cloneList(ArrayList<Integer> list) {
    ArrayList<Integer> clone = new ArrayList<Integer>(list.size());
    for (int i=0;i<list.size();i++){
        clone.add(list.get(i));
    }       
    return clone;
}

Small Change: this is the method used when making a change to the copied arraylist

public static ArrayList<Integer>smallChange(ArrayList<Integer>oldArray){
    int firstNo;
    int secondNo;

    do{
        Random rand1 = new Random();
        Random rand2 = new Random();
        firstNo = (int) Math.abs(matrix_length*rand1.nextDouble());
        System.out.println("A: "+firstNo);
        secondNo = (int) Math.abs(matrix_length*rand2.nextDouble());
        System.out.println("B: "+secondNo);

    }while(firstNo == secondNo);


    int temp1 = oldArray.indexOf(firstNo);
    int temp2 = oldArray.indexOf(secondNo);

    oldArray.set(temp1, secondNo);
    oldArray.set(temp2,firstNo);
    ArrayList<Integer> newArrayList = oldArray;             

    return newArrayList;        
}
Rishi Kapadia
  • 61
  • 1
  • 6
  • 7
    There is no way the changes in one are reflected in the other: Integer objects are inmutable: http://stackoverflow.com/questions/4117793/are-java-wrapper-classes-really-immutable – Pablo Lozano Apr 07 '17 at 13:09
  • could you try with `ArrayList` instead? – Patrick Ferreira Apr 07 '17 at 13:09
  • 2
    @PatrickFerreira There's no such thing as `ArrayList` – khelwood Apr 07 '17 at 13:10
  • 1
    @PatrickFerreira No, a List can only contain objects, no primitives – Pablo Lozano Apr 07 '17 at 13:10
  • 1
    @PatrickFerreira only objects is allowed in `ArrayList` – 0xDEADBEEF Apr 07 '17 at 13:11
  • `java.util.ArrayList` (like many other standard collections) implements Cloneable, so `(List) list.clone()` will create a clone of the original list. However, the prefered way of creating a copy of a list, is to use the copy-constructor: `new ArrayList(list)`. – Robin479 Apr 07 '17 at 13:12
  • 4
    It's hardly a pain. `new ArrayList(list)` is all that's needed to copy a list. – khelwood Apr 07 '17 at 13:12
  • Immutability has nothing to do with memory references – bichito Apr 07 '17 at 13:16
  • 2
    @efekctive Immutability does have something to do with whether *changes* can be reflected across other references though. – khelwood Apr 07 '17 at 13:19
  • Sorry, the problem as described happens because lists hold references to objects, it has nothing to do with the immutability of its elements. The replaced immutable object will be gc'ed at a later point – bichito Apr 07 '17 at 13:21
  • @Pablo how can I get around it being immutable, do I have to create my own object? – Rishi Kapadia Apr 07 '17 at 13:23
  • 1
    Your deep copy works fine. Please post the code you are using to test it ("make any changes to one copy, those changes are reflect in the other"): I think you might have a bug in THAT. – David Lavender Apr 07 '17 at 13:24
  • @RishiKapadia Your problem is in another part of your code, just find where you are modifying the original list. There is no other option – Pablo Lozano Apr 07 '17 at 13:26
  • @efekctive Nothing in the question is anything to do with garbage collection. – khelwood Apr 07 '17 at 13:26
  • I am not talking about gc. I am talking about what happens to the immutable object that is not being referenced by anybody when replaced. This problem is about lists not immutability – bichito Apr 07 '17 at 13:28
  • If op uses new Integer(...) with the second list, the changes will not echo across lists – bichito Apr 07 '17 at 13:32
  • @efekctive Since `Integer` is immutable, changes to instances of `Integer` cannot **possibly** be reflected across lists, because there can be no changes to `Integer`. – khelwood Apr 07 '17 at 13:36
  • 2
    @RishiKapadia ABout the update: You are modifying the old arrayList, I don't know what you are expecting there – Pablo Lozano Apr 07 '17 at 13:38
  • This is my last comment: lists do not hold objects hold references to objects. that is why changes in one list appear in another. What is changing is the list not the Integer. – bichito Apr 07 '17 at 13:39
  • 1
    @efekctive Create a small test and check how pointers work, you are missing something – Pablo Lozano Apr 07 '17 at 13:42
  • Test done. Please accept the answer – bichito Apr 07 '17 at 13:47
  • I deleted my answer because it is not worth the discussion. The general consensus in this thread is that two lists holding references to the same objects will not reflect changes across. That is impossible. The solution is to create a new object in the other list – bichito Apr 07 '17 at 13:57
  • @efekctive You are confusing list changes with element changes. It makes sense to copy the list, because a list might be changed. It is unnecessary to copy the individual `Integer`s, because they cannot be changed. – khelwood Apr 07 '17 at 14:14
  • I am off this thread. – bichito Apr 07 '17 at 14:26
  • @efekctive My example: http://www.tutorialspoint.com/compile_java_online.php?PID=0Bw_CjBb95KQMeDNEMy05b3FqcG8 – Pablo Lozano Apr 07 '17 at 15:50
  • I am off this thread – bichito Apr 07 '17 at 15:55

4 Answers4

0

Is it possible to create an independent copy of an integer array list in java?

you could simply do this:

public static ArrayList<Integer> cloneList(ArrayList<Integer> list) {
      return new ArrayList<Integer>(list);
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    unmodifiableList doesn't produce a clone but just a view of the list that can't be modified (https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#unmodifiableList(java.util.List)) – muzzlator Apr 07 '17 at 13:17
0

Try the Collections.copy method.

public static void copy(List dest, List src)
iPhantomGuy
  • 240
  • 1
  • 11
0

If I understand what you are really trying to do (creating a new ArrayList with a random small change), the problem is that you are modifying the original list and then cloning it. Assuming that, here is a solution:

public static ArrayList<Integer> smallChange(ArrayList<Integer> oldArray) {
    int firstNo;
    int secondNo;

    do {
        Random rand1 = new Random();
        Random rand2 = new Random();
        firstNo = (int) Math.abs(matrix_length*rand1.nextDouble());
        System.out.println("A: "+firstNo);
        secondNo = (int) Math.abs(matrix_length*rand2.nextDouble());
        System.out.println("B: "+secondNo);

    } while(firstNo == secondNo);

    ArrayList<Integer> newArrayList = new ArrayList<>(oldArray);
    int temp1 = newArrayList.indexOf(firstNo);
    int temp2 = newArrayList.indexOf(secondNo);

    newArrayList.set(temp1, secondNo);
    newArrayList.set(temp2,firstNo);    
    return newArrayList;        
}

And here is an exaple of having a list copied and then having the copy modified:

import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        List<Integer> list1= new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        List<Integer> list2= new ArrayList<>(list1);
        list2.set(1,6);
        System.out.println(list1);
        System.out.println(list2);
     }
}
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
-1

You can use unmodifiableList method of Collections class which will at least stop modification in list object which is pass as parameter in unmodifiableList method of Collections class.

public List<String> getList(List<String> alist) {
        List<String> list = new ArrayList<String>(alist);
        List<String> unmodifiableList = Collections.unmodifiableList(list);

        return unmodifiableList;
    }
Rahul Rabhadiya
  • 430
  • 5
  • 19