Results 1 to 9 of 9
  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default inserting values in to array

    package secondDay;

    import java.util.Arrays;

    public class Mgr
    {
    public static void main(String[] args)
    {
    int x[] = new int[5];
    int y[] = new int[10];
    x=y;
    y=x;
    System.out.println(Arrays.toString(x));
    System.out.println(Arrays.toString(y));
    System.out.println("x values"+Arrays.toString(x));
    System.out.println("y values"+Arrays.toString(y));
    }
    }

    Hi,
    here i want to insert the values into array x and array y
    i.e i want to see wheather array x values are getting refelcted in the y array or not

    so can any one please help me how to insert the values int array x with out changing the above code


    Thanks in advance
    Daya

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can either use array indexing and the assignment operator
    x[1] = 10;

    Or you can instantiate the array with values upon declaration

    Java Code:
    int[] x = {1, 4, 35, 5};

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Dayanand View Post
    so can any one please help me how to insert the values int array x with out changing the above code
    Without changing the code? That's going to be quite hard to do.

  4. #4
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    int[] x = {1, 4, 35, 5};

    for this code,it is stating a error "duplicate variable x"
    Last edited by Dayanand; 03-05-2011 at 02:32 PM. Reason: better

  5. #5
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Without changing the code? That's going to be quite hard to do.
    its ok sir, i want to learn how to insert the values with out changing above the code

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Dayanand View Post
    its ok sir, i want to learn how to insert the values with out changing above the code
    This makes no sense at all for if you want a program to have different behavior, you must change its code. Otherwise how is this supposed to work? Magic?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you declare x put the line I gave instead.

    Instead of
    Java Code:
    int x = new int[5];
    Use the line I put. You could also do the following AFTER declaring x
    Java Code:
    for(int j = 0; j < x.length(); j++){
      x[j] = j;
    }
    this is called looping through the array and is pretty fundamental to programming. Finally, you must change the code a little to add values into the array.

  8. #8
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Dayanand View Post
    x=y;
    y=x;
    Am I missing something about the assignment of arrays in Java or is this as bad as it looks? To me it looks like the array originally assigned to x will be lost and both x and y will be references to the array originally assigned to y.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As far as I understand this is correct, I don't believe the op was intending for the program to be useful, and instead he was simply trying to see how things happened.
    Last edited by sunde887; 03-05-2011 at 04:25 PM.

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. Inserting input into array
    By brmcdani in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 02:33 PM
  4. inserting values from jtable into database
    By sandeepsai17 in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 07:31 PM
  5. Replies: 0
    Last Post: 12-24-2007, 09:47 AM

Posting Permissions

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