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 4 of 4

Thread: Copying arrays from one class to another

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

    Default Copying arrays from one class to another

    I'm trying to create a method in one class to fill up an array with double values, and then I not only need to call that method in another class, but I need to be able to assign those double values to a new array in the second class. I have to be able to iterate through the double values and subtract them from a total, and so far I haven't found a good way to do that. Is it possible to copy an array into an array in a new class, or do I need some other sort of trick?

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Copying arrays from one class to another

    Is it possible to copy an array into an array in a new class,
    Yes. What you need to consider is if you want a copy of the contents of the array or if you just need a reference to the one array to be able to access that array from another class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copying arrays from one class to another

    That's the thing, I know how to access an array from another class, but I need to actually copy the contents, and I can't figure out how to do that.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Copying arrays from one class to another

    how to access an array from another class
    Do you know how to access a variable in one class from another class? An array is just another type of variable.

    copy the contents,
    There are several ways:
    use a loop to copy element by element
    use the clone() method: newArray = oldArray.clone()
    use the Arrays class's copyOf() method
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Do Class Casts Work With Arrays?
    By snowguy13 in forum Collections and Generics
    Replies: 8
    Last Post: February 23rd, 2012, 07:18 AM
  2. [SOLVED] Arrays being initialized from another class
    By Java Man 9000 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 11th, 2012, 03:34 PM
  3. Copying Arrays
    By AnnexTrunks in forum What's Wrong With My Code?
    Replies: 30
    Last Post: October 24th, 2011, 10:04 PM
  4. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM
  5. Issue with setting up different class with arrays
    By D-COD3R in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 06:09 PM