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.

Copying Arrays

I will paste my code below, but what I have so far is an array declared, named it "book", did some loops to get some user input and then display the outcome, and that works fine. I was told when you are copying an array, you create a new object and that's where I am stuck. I don't understand how to create and initialize this new object in order to copy my array. I hope I have the right idea at least lol.

That first part, before the equals sign, is all that's meant by creating a new object. copyOf() returns an array, a NEW array, so you have to have a name to put it in, such as "copiedBook". As a general hint for using those method summary pages, always look at the far left column - it tells you what the method is going to return.

That's for copying an ARRAY to an ARRAY, and what you have is an ArrayList, which might not work. So, now you have to work out how to turn an ArrayList into an Array. You might look here: ArrayList (Java 2 Platform SE v1.4.2)

Re: Copying Arrays

PhoneBookEntry[] copiedBook = Array.copyOf(book, book.length);

Well, you must look into ArrayList in detail. There is difference between arrays and arraylist.
You are trying to actually copy two arrays, not arraylists and your book is an arraylist.
Well, if you want to copy an arraylist into the other, you must use this;

First_ArrayList.addAll(Second_ArrayList);

I will recommend you to go through arraylist topic in detail.
Good luck

My first array PhoneBookEntry works fine with my While loop, and if statement. Creating and copying my second arraylist is where I am having trouble. My understand is that, I create two array lists, I get info and fill one of them. The second one remains empty, until I want to copy and fill it in right? There should be some method for that I use, then I can create my while loop and if statement to display the contents of the second array. Is that all right?

Re: Copying Arrays

Originally Posted by AnnexTrunks

I've been tinkering with my code some more, and here is what I got.

//*****************************Copied Object*******************************\\ <--------Here is where I made a "second empty" array. I get errors in netbeans though.//Copy of the primary object
PhoneBookEntry[] copiedBook =ArrayList.copyOf(book, book.length);System.out.println("Name"+"\tPhone Number");for(int i =0; i < copiedBook.size(); i++){System.out.println(copiedBook.get(i).getName()+"\t"+ copiedBook.get(i).getPhoneNumber());}}}

1) My first array PhoneBookEntry works fine with my While loop, and if statement.

2) Creating and copying my second arraylist is where I am having trouble.

3) My understand is that, I create two array lists, I get info and fill one of them.

4) The second one remains empty, until I want to copy and fill it in right?

5) There should be some method for that I use,

6) then I can create my while loop and if statement to display the contents of the second array. Is that all right?

OK, first lets go through the words I broke up your par into thoughts.
1) great

2) to copy a list you need to Iterate through the list if its a primitive array then for each or for statement will work

3) your thoughts here are absolutely correct.

4) This you can do

5) why does there have to be a method??? You can create one its easy...

Re: Copying Arrays

In my second for loop, the .size, and .get give me red squiggly lines. I just copied from my first for loop, but I substituted the array names. I then tried copying your provided code into mine to try it out and it was ALL red lol. Sorry if I am being difficult, but I am still earning. When I get errors, I don't know enough information to fix the problems lol.

Re: Copying Arrays

Originally Posted by AnnexTrunks

In my second for loop, the .size, and .get give me red squiggly lines. I just copied from my first for loop, but I substituted the array names. I then tried copying your provided code into mine to try it out and it was ALL red lol. Sorry if I am being difficult, but I am still earning. When I get errors, I don't know enough information to fix the problems lol.

Sometimes difficult is frustrating, however you never learn unless you ask the question.

Ok lets go over some of the Array info

double[] d =newdouble[5];//<-- you have to declare a [b]size[/b]
ArrayList<Double> d =new ArrayList<Double>();//<---- here you dont have to declare a size but you call [b] d.length[/b]

also Double is a Object
double is a primitive.

to get the data from a Double you have to parse it double value = Double.parseDouble( Obj.class ) ;

double[] d needs a loop of some sort example for loop,

for(int i =0; i < d.getSize();i++){System.out.println(" in my array my double is : "+ d[i];// notice the i is the counter in this array you have to have it...}// end for loopIterator iter = d.iterator();// my netbeans is shut down i pretty sure this is right for ArrayList Class

the code i wrote before is just an example and was not intended for copy paste.

now all you really have to do is to make a loop and assign the data in the first ArrayList to the second one in a single loop as well as use a print statement before middle and after to see what your doing...

those println() are really helpful all the time. It gives you an idea what is happening.... I have so many println() in my code its hard to read but then again I also use the break and debug in netbeans to see values when i have a lot.

Re: Copying Arrays

If you scroll up in my original post, you will see an Array declared, and PhoneBookEntry is the other class file I am referencing to. That file in itself is complete and functional. So is the original array and all my loops. My editor and myself does not run into problems until I start trying to make a copy of the array.

I feel like I'm so close to this, but it's so frustrating. I can't stop messing with it either because tomorrow I go in for testing lol. A break is needed but I don't have time.

I just need to somehow make a copy of my first array, and display the information. What sucks is that the problem I am having is probably SO small to you guys, and I'm going to feel like a putz for not seeing how to fix it.

Re: Copying Arrays

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:

Re: Copying Arrays

Don't send me private messages. Ask your questions on the forum so everyone can see them and try to help.

The reason you cannot find any info about the copyOf method in the ArrayList class is because there isn't one. The copyOf methods belong to the Arrays class. There is an example of how to use it in reply 15.