Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: array elements assignment question

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array elements assignment question

    Hello more experienced users,
    Can someone explain to me how it is that this code prints 012? Do arrays behave differently than ints and floats in that when you declare anyType[]1=anyType[]2, the elements of anyType[]1 are assigned to match the values of anyType[]2 throughout the rest of the method?

    public class test {

    public static void main(String[] args){
    int[]list1={1,2,3};
    int []list2={1,2,3};
    list2=list1;
    list1[0]=0;list1[1]=1;list2[2]=2;

    for(int i=0;i<list2.length;i++)
    {
    System.out.print(list2[i]+"");
    }

    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: array elements assignment question

    how it is that this code prints 012?
    These statement assigns those values to the array:
    list1[0]=0;list1[1]=1;list2[2]=2;
    This statement replaces the value that was in list2 with the value that is in list1
    list2=list1;
    Now there are two variables that point to the same array. The values that were in the array originally pointed to by list2 are lost.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] matching elements of one array with another array.
    By shenaz in forum Java Theory & Questions
    Replies: 13
    Last Post: March 29th, 2013, 10:31 AM
  2. How to sum up elements in an array?
    By D-X69 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2012, 01:21 AM
  3. Help with assignment about a board game (enum and array question)
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2012, 07:47 AM
  4. Help camparing array elements
    By Richmond in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 12:23 AM

Tags for this Thread