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.

Page 2 of 2 FirstFirst 12
Results 26 to 31 of 31

Thread: Copying Arrays

  1. #26
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copying Arrays

    Quote Originally Posted by Junky View Post
    One thing you have to get clear is that copying an array does not automatically mean that you will have two unique arrays if objects are involved. Depending upon how you do it, both arrays probably refer to the same objects. Consider the following code:
    import java.util.Arrays;
     
    class Juju {
        private String value;
     
        Juju(String s) {
            value = s;
        }
     
        public String toString() {
            return value;
        }
     
        public void change(String s) {
            value = s;
        }
     
        public static void main(String[] args) {
            Juju[] one = {new Juju("one"), new Juju("two"), new Juju("three")};
            Juju[] two = Arrays.copyOf(one, one.length);
            one[0].change("ten");
            System.out.println(two[0]);
        }
    }
    Even though the code changes the value of the object in array one the output is "ten" because array two also refers to the same object.
    Ok, I began typing my second array....when I reach.....""Array." once I type in the period, a small list of java methods come up. A LOT of copyOf comes up. Which one should I use?

  2. #27
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Copying Arrays

    It doesn't matter. Simply type the method name and then add your 2 parameters: your array and an int indicating the length of the new ("copy") array.
    Improving the world one idiot at a time!

  3. #28
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copying Arrays

    Hmmm, when choosing a copyOf method, I chose the very first option...followed thru with code...and no matter what I seem to do....I get a squiggly line underneath my second parameter. Whether I choose, book.size, book.length(doesnt recognize lenght)....I get syntax error. Argh, I feel so close!!!

     ArrayList <PhoneBookEntry> book = new ArrayList <PhoneBookEntry>();          <-----my first arraylist
     ArrayList <PhoneBookEntry> copiedBook = new ArrayList <PhoneBookEntry>(); <------ the 2nd list in which I wish to copy the first
     copiedBook = Arrays.copyOf(book, book.size());    <-------------------------the squiggly line appears under the phrase book.size.

  4. #29
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Copying Arrays

    I thought you said you wanted to make a copy of an array but in your code you are using ArrayLists. make up your mind.
    Improving the world one idiot at a time!

  5. #30
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copying Arrays

    I do want to copy the contents of one ArrayList, into another. I guess I didn't see what I wrote.

  6. #31
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Copying Arrays

    So a lot of time has been wasted due to you not posting correct information. The ArrayList class has a clone method but you will still have the same issues that I mentioned in reply 15.
    Improving the world one idiot at a time!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 02:41 AM
  2. copying and comparing stacks
    By colerelm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 01:22 AM
  3. [SOLVED] Problem copying an arraylist to an array
    By allhalf425 in forum Collections and Generics
    Replies: 4
    Last Post: September 20th, 2011, 10:25 AM
  4. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 11:01 AM
  5. need help copying (what is in address bar)
    By 3ammary in forum Java Networking
    Replies: 0
    Last Post: January 8th, 2011, 08:32 AM