0

I have many functions that take a global ArrayList as an argument, some of them don't make any change of this list, and others are need to remove some elements of this array while working, so i create a local tempArrays inside these function.

static ArrayList array1 = new ArrayList();

public fn1(ArrayList array1)
{
   ArrayList tempArray1 = new ArrayList();
   tempArray1 = array1;
   tempArray1.remove(elemnt);
}

The problem is the deleted elements is deleted also from original arrayList array1, i don't know why? .

Thanks..

Mahmoud
  • 197
  • 1
  • 5
  • 16
  • 1
    That's because arraylist is reference type and your new arraylist reference the outer one. Any changes to the new one will be reflected to the outer one. – Nick Apr 27 '13 at 19:38

8 Answers8

6

In line :

tempArray1 = array1;

You are making the tempArray1 variable to refer the same object of ArrayList to which array1 is referencing. Hence both are referencing the same object of ArrayList. Any change made in ArrayList object using any of the variables, would be reflected by both variables. So when you removing an element using:

tempArray1.remove(elemnt);

array1 is also reflecting that removal of element.

You should use:

ArrayList tempArray1 = new ArrayList(array1);

As specified in official documentation : ArrayList(Collection<? extends E> c) :

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters: c - the collection whose elements are to be placed into this list

Vishal K
  • 12,976
  • 2
  • 27
  • 38
4

The problem is that declaring tempArray1 = array1; simply makes tempArray1 a reference to array1. To create a true copy, call this:

ArrayList tempArray1 = new ArrayList(array1);
sleeparrow
  • 1,242
  • 13
  • 22
Jared Nielsen
  • 3,669
  • 9
  • 25
  • 36
2

try to do the following

List tempList = new ArrayList(oldArrayList);
WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
2

The reason why the item is deleted from the original ArrayList is because, in Java, variables of reference types (arrays, Strings, and anything else which is a subclass of Object) hold references, not values. What that essentially means is that a variable of type ArrayList holds the memory address at which the data for the ArrayList is allocated. When you do tempArray1 = array1;, you're just giving tempArray1 the address of the same ArrayList. This all only applies to types that derive from Object in Java, which does happen to include String, if you didn't know. Primitive types (byte, short, int, long, char, float, double) are stored as values.

Now, onto your real problem; I must ask, why you don't just use the get(index) method to work with the item?

If you need to actually remove the element, use the copy constructor of ArrayList, like so: ArrayList tempArray1 = new ArrayList(array1);

sleeparrow
  • 1,242
  • 13
  • 22
2

I think there are some fatures of Java that you have misunderstood:

  1. When you declare a Java variable, it is a refenrence for an object, like a pointer in C/C++. It can be a referance to previously created object or you can create a new object to referance.

    //Created a new object instance on heap
    ArrayList tempArray1 = new ArrayList();
    //Both tempArray1 and tempArray2 will point to same object in memory
    ArrayList tempArray2 = tempArray1;
    

    In your code you first creating a new ArrayList object referance tempArray1 and creating an a new object for it ArrayList tempArray1 = new ArrayList(); and than assigning an other referance to this referance by tempArray1 = array1;. So first object you have created has no reference that point it and will be deleted. Now both tempArray1 and tempArray2 are pointing the same oject.

  2. In Java If you want to make a copy of an object you can use it's clone method if it has implemented the Clonable interface. Also look at this question for detailed answers about cloning How do I copy an object in Java? So you want to make a copy of your array but you are doing it wrong.

Community
  • 1
  • 1
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
1

You are replacing the instance you created with the global one. Try with this:

ArrayList tempArray1 = new ArrayList(array1);
tempArray1.remove(elemnt);
fbiagi
  • 766
  • 6
  • 11
1

You can also use the addAll(Collection c) Method

static ArrayList array1 = new ArrayList();

public fn1(ArrayList array1)
{
   ArrayList tempArray1 = new ArrayList();
   tempArray1.addAll(array1);
   tempArray1.remove(elemnt);
}
Matthias
  • 1,200
  • 2
  • 13
  • 33
1

A) tempArray1 is a variable that refers to the same instance as array1. To make a copy, use the copy constructor:

List tempArray1 = new ArrayList(tempArray1);

B) Remove the parameter from the method. There is no need to pass the list in - code in the method already has access to the list. In fact, by passing it in using a parameter with the same name, you are shadowing the original list - if a different list was passed in, you would be working with that instead of the list you think you're working with.

C) You should type your list, for example:

static ArrayList<String> array1 = new ArrayList<String>();

D) Always use the abstract class: change it to List:

static List<String> array1 = new ArrayList<String>();

E) A list is not an array, so don't call the variable "array1" - its misleading:

static List<String> list1 = new ArrayList<String>();
Bohemian
  • 412,405
  • 93
  • 575
  • 722