8 Replies Latest reply: May 8, 2013 8:50 AM by 851498 RSS

    Array

    851498
      Is it possible to compare 2 arrays as below if they have same size?
      Int array[][] = new int[10][10];
      array1[I] == array2;

      Or assign the whole array to another array?
      array1[I] = array2[I];


      Suppose I have 2 arrays, 1 is old, and 1 is new.
      I will do the calculation to update the new array.
      Then, I will compare between the 2 arrays.

      If they are the same, I will stop the calculation.
      If they are not the same, I will assign the new array to the old array.
      Then, I will do the calculation again until the old array and the new array are the same.

      As I do not want to write a loop to compare and assign the value to the arrays.
      May I compare or assign the values to the whole array directly.
      I am appreciated if anyone could help. Many thanks.
      Edited by: BusMan29 on May 7, 2013 8:24 PM

      Edited by: BusMan29 on May 7, 2013 8:29 PM

      Edited by: BusMan29 on May 7, 2013 9:31 PM

      Edited by: BusMan29 on May 7, 2013 9:35 PM

      Edited by: BusMan29 on May 7, 2013 9:37 PM

      Edited by: BusMan29 on May 7, 2013 9:38 PM
        • 1. Re: Array
          gimbal2
          Don't see it as one array, its an array of arrays - a nested structure. So you don't have one array here, you have 11 of them - one outer array and 10 child arrays. You just conveniently initialize all of them at once because Java is awesome, you could have also done this:
          int[][] array = new int[5][];
          array[0] = new int[5];
          array[1] = new int[10];
          array[2] = new int[15];
          array[3] = new int[20];
          array[4] = new int[25];
          And now all the child arrays have a different size, to make comparison more difficult. On top of that, one of the arrays can easily be null.
          int[][] array = new int[5][];
          array[0] = new int[5];
          array[1] = new int[10];
          array[2] = null;
          array[3] = new int[20];
          array[4] = new int[25];
          And this also works:
          int[][] array = new int[10][10];
          array[5] = null;
          array[5][0] = 10; // crash
          Now imagine the steps you have to take to reliably compare or copy such a nested structure.
          • 2. Re: Array
            851498
            Maybe I have given you a wrong message.
            Simply, suppose we have 2 arrays, they have the same size,could we directly compare them or assign the values to them?
            • 3. Re: Array
              gimbal2
              BusMan29 wrote:
              Maybe I have given you a wrong message.
              Simply, suppose we have 2 arrays, they have the same size,could we directly compare them or assign the values to them?
              But then you would talk about a normal array, like this:
              int[] array1 = new int[10];
              int[] array2 = new int[10];
              You can easily compare the size:
              if(array1.length == array2.length){
                // bla
              }
              and you can also copy over values using System.arraycopy(), or just using a for-loop yourself.

              http://www.tutorialspoint.com/java/lang/system_arraycopy.htm
              • 4. Re: Array
                doremifasollatido
                BusMan29 wrote:
                Is it possible to compare 2 arrays as below if they have same size?
                 int array[][] = new int[10][10];
                array1[I] == array2;

                // Or assign the whole array to another array?
                array1[I] = array2[I];
                  
                 
                
                Suppose I have 2 arrays, 1 is old, and 1 is new.
                I will do the calculation to update the new array.
                Then, I will compare between the 2 arrays.

                If they are the same, I will stop the calculation.
                If they are not the same, I will assign the new array to the old array.
                Then, I will do the calculation again until the old array and the new array are the same.

                As I do not want to write a loop to compare and assign the value to the arrays.
                May I compare or assign the values to the whole array directly.
                I am appreciated if anyone could help. Many thanks.
                To do the comparison, take a look at java.util.Arrays.deepEquals. If I understand your intention correctly, you can assign the new array (array2) to the old (array1), and then create a new blank array for the new (array2):
                array1 = array2;
                array2 = new int[10][10];
                This will only do what (I think) you want if your new array can start out blank, instead of starting out with the "old" values. If you need the new array to start out with the "old" values, you will need to manually copy the array (potentially with the help of System.arraycopy, as a previous poster mentioned [note that even with System.arraycopy, you need at least one loop yourself---if you use arraycopy on the outer array and not each inner array, you will not get two independent 2-dimensional arrays]).
                Then do a new calculation in array2, then repeat the "assign and create a new array" process.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                • 5. Re: Array
                  851498
                  Thanks gimbal2 & doremifasollatido,

                  Do I have any problem, if I have initialized an array and I initialize an array again?
                  • 6. Re: Array
                    851498
                    Actually, I would like to compare the value of the arrays and not the size.
                    I know I could use a loop to do it.
                    But may I directly compare them by:
                    array1 == array2;
                    • 7. Re: Array
                      gimbal2
                      BusMan29 wrote:
                      But may I directly compare them by:
                      array1 == array2;
                      Of course not, basic Java knowledge should have told you that. You can't value-compare two objects using the equals operator.

                      In my opinion it is more valuable to learn how to get your own information rather than getting it handed to you directly. And getting "how do I do this" information is easy: Google is your friend. Try a search for "java array compare". In less than a minute of reading effort I found out there is an Arrays.deepEquals().

                      http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html
                      • 8. Re: Array
                        851498
                        I have got the answer.
                        Thank for your reply.