Home > other >  Java - How to replace entire 2D ArrayList with another that has different size
Java - How to replace entire 2D ArrayList with another that has different size

Time:05-15

I have 2D ArrayList consisting of Objects, i pass it to a function in another class, like this:

public void function(ArrayList<ArrayList<Object>>)..

class.function(array)...

then inside of this function i create another 2D ArrayList (with Object type), but this one has different size and amount of values and values themself are different.

How to "assign" this new ArrayList to the one i passed in to a function, so that the passed one becomes exactly like that new one created in a function? And remains the same outside the function of course.

Btw im doing this inside of a function:

ArrayList<ArrayList<Object>> ArrayNew = new ArrayList<ArrayList<Object>>();
                        
for(int i=0; i < y; i  ) {
    ArrayList<Object> temp = new ArrayList<Object>();
    ArrayNew.add(temp);
}

and then i'm adding new values in some way like this:

ArrayNew.get(some_value_that_changes_and_is_lower_than_y).add(tempObject);

Effect identical to

Array = ArrayNew

CodePudding user response:

In Java, you can't reassign a method parameter and have it also reassign the caller's reference. That is just not possible in Java. You either have to modify the passed-in list or return the new list (which you would assign to the variable you want to replace).

Modify passed-in list:

// function(list);
public void function(List<List<Object>> list) {
  int y = list.size();
  list.clear();
  for (int i = 0; i < y; i  ) {
    list.add(new ArrayList<>());
  }
}

Return the new list:

// list = function(list);
public List<List<Object>> function(List<List<Object>> list) {
  List<List<Object>> newList = new ArrayList<>();
  for (int i = 0; i < list.size(); i  ) {
    newList.add(new ArrayList<>());
  }
  return newList;
}

I don't really know what you're trying to do with the list, so I just made something up close to what your example code does.


Couple notes:

  1. You should use standard Java naming conventions, at least when posting to a public forum such as Stack Overflow. Variable names use camelCase (first letter lowercase). Class/interface names use CamelCase (first letter uppercase).
  2. Unless you need something specific to an implementation, typically you want to declare the types of parameters/variables as the interface (List instead of ArrayList, in this case).
  3. You don't need to declare the generic type arguments on both sides of the = operator, at least not since Java 7.

CodePudding user response:

Assuming your current code looks something like this:

public void foo(ArrayList<ArrayList<Object>> array) {
    ArrayList<ArrayList<Object>> newArray = new ArrayList<>();
    /* fill newArray with data */
    array = newArray; // this will have no effect outside this method
}

public void main() {
    ArrayList<ArrayList<Object>> mainArray; /* your outer 2D arrayList */
    foo(mainArray);
}

To understand why foo does not change the mainArray object, you can think of calling a method as simply copying its code to your method. Like this:

public void main() {
    ArrayList<ArrayList<Object>> mainArray; /* your outer 2D arrayList */
    // foo(mainArray); gets replaced with
    ArrayList<ArrayList<Object>> array = mainArray;
    ArrayList<ArrayList<Object>> newArray = new ArrayList<>();
    /* fill newArray with data */
    array = newArray;
}

Unsurprisingly, the mainArray remains unchanged, and array refers the same instance as newArray.
As Slaw pointed out, you can either edit the instance mainArray refers to, or make mainArray refer to the new instance instead.
But I would suggest first asking yourself: why do want to edit mainArray in the first place?
It sounds like there might be multiple places in your code referencing mainArray, in which case you should consider refactoring your code.
You should also check that you don't have any references to the inner lists of your mainArray, as those will not reflect the changes you make to the outer list.

  • Related