Results 1 to 2 of 2
  1. #1
    hatela150 is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Question Aliasing In 2d Arraylist

    HELLO EVERYONE,

    Could anyone please help me with this java question? I am copying a 2d arraylist and making changes to the copy. For some reason the changes are also applied to the original copy of the arraylist. I need an original copy of my arraylist, too. The following is my code please go through it. The code looks realy long, its actualy very simple and easy to understand.

    Thanks in advance.


    import java.util.ArrayList;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    //import java.awt.RenderingHints.*;


    class test {
    public test() {
    test2();
    }

    public static void main (String [] param) {
    new test();

    }

    public void test2() {
    int num=0;

    ArrayList [][] array1 = new ArrayList [6][6];


    for (int x = 0; x < array1.length; x++)
    {
    for (int y = 0; y < array1[x].length; y++)
    {
    array1[x][y] = new ArrayList();

    }
    }
    /*The following for loop is fillling the arraylist*/
    for (int i=0; i < array1.length; i++) {
    for (int j=0; j<array1.length; j++) {

    if (((num/2)*2)==num) {
    // num = -1;
    array1[i][j].add("s");
    }

    if (((num/2)*2)!=num) {
    // num = +1;
    array1[i][j].add("h");
    }
    num++;
    }
    num=num+1;
    }//end ou



    /*The following for loops just prints the 2d arraylist on to the board*/
    int k=0, l=0;
    System.out.println("----------------------\n");
    for (k=0; k<array1.length; k++) {
    for (l=0; l<array1.length; l++) {
    System.out.print(array1[k][l]);

    }
    System.out.print("\n");
    }
    System.out.println("----------------------\n");


    test1(array1); // passing

    } // end methos test2

    public void test1(ArrayList [][] arrayCopy) {
    ArrayList [][] array2 = new ArrayList [6][6];
    int m=0, n=0;
    array2 = arrayCopy.clone();

    System.out.print("\ntest" + arrayCopy[0][1]);

    for (m=0; m<arrayCopy.length; m++) {
    //array2[m] = arrayCopy[m];
    for (n=0; n<arrayCopy.length; n++) {
    //array2[m] = arrayCopy[m];
    array2[m][n].addAll(arrayCopy[m][n]);
    // array2[m][n].add(arrayCopy[m][n].get(0));
    }
    }


    array2 [1] [1].add("w");
    array2 [0] [0].add("q");

    // this is where i make changes to the copy of arraylist and print the copy of array
    System.out.println("changes made to the copy of array----------------------\n");
    for (int k=0; k<array2.length; k++) {
    for (int l=0; l<array2.length; l++) {
    System.out.print(array2[k][l]);

    }
    System.out.print("\n");
    }
    System.out.println("----------------------\n");


    // this is where i print the original array to see if changes has effected it.
    System.out.println("this should be original array----------------------\n");
    for (int k=0; k<arrayCopy.length; k++) {
    for (int l=0; l<arrayCopy.length; l++) {
    System.out.print(arrayCopy[k][l]);

    }
    System.out.print("\n");
    }
    System.out.println("----------------------\n");

    } // end method test1

    } // end class

    :confused:

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    The problem is that the clone() method only makes what is called a "shallow copy" of your array. So you get a new 6x6 array, but each ArrayList inside that array is the selfsame ArrayList as in the corresponding position of the original.

    To solve your problem just create a new array (don't bother with clone), and then in each position actually put a copy of the ArrayList in the corresponding position from the first array. To make the copy of the ArrayList, remember you can just create a new ArrayList, passing in the list you want it to be a copy of.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 01:47 AM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 09:48 PM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 08:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •