Results 1 to 3 of 3
  1. #1
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default Question about array

    I wanna know are the following cases implement the same thing?

    Case A.
    Java Code:
    int arrayA[] = new int[10];
    int arrayB[] = new int[10];
    
    arrayA = arrayB;
    Case B.
    Java Code:
    int arrayA[] = new int[10];
    int arrayB[] = new int[10];
    
    for (int i = 0 ; i < arrayA.length; i++)
    {
         arrayA[i] = arrayB[i];
    }
    In both case, I found that arrayA contains the same elements as arrayB.
    However, is there any difference(meaning or implementation.....) between these two cases?
    Thanks for your help:)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    No they are different.
    arrayA = arrayB;
    The above replaces/loses the array arrayA used to point to, replacing it with a reference to arrayB.
    If you make a change to arrayA it can be seen in arrayB because there is only one array.
    In the second case if after the loop you make a change to arrayA it will NOT be seen in arrayB.
    Write a small test program to see this.

    In both case, I found that arrayA contains the same elements as arrayB.
    Exactly. arrayA points to the SAME array as arrayB. The initial contents of arrayA are gone.

    Your code is the same as:
    String s1 = "1";
    String s2 = "2";
    s1 = s2; // the reference to "1" is replaced with one to "2". The "1" is gone.
    Last edited by Norm; 09-18-2010 at 03:38 PM.

  3. #3
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

Similar Threads

  1. Array question
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 06-11-2010, 01:41 AM
  2. Array Question
    By sc001 in forum New To Java
    Replies: 1
    Last Post: 02-14-2010, 04:57 AM
  3. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 05:44 AM
  4. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 AM
  5. file/ array question
    By mayhewj7 in forum New To Java
    Replies: 10
    Last Post: 02-18-2009, 03:54 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
  •