0

i have a question and i need some help please.

i have an object, for example Contact , with String name, String adsress and String phone variables

I create an array of this object

Contact[] homeContacts = new Contact[10];
homeContacts[0] =  new Contact();
...
...
homeContacts[9] =  new Contact();

And i populate every variable like this

homeContacts[0].name="jim"
homeContacts[0].adsress ="Oak road 52"
homeContacts[0].phone="555-6363633"

and i continue with homeContacts[1] and so on

now lets say i create a second array Contact[10]

Contact[] workContacts = new Contact[10];
workContacts[0] =  new Contact();
...
...
workContacts[9] =  new Contact();

and i copy the values of homeContacts to WorkContacts massively like this

workContacts=homeContacts;

Now every variable of homeContacts array has the same reference with the value of workContacts array! if i change for example a name of work homeContacts[0].name="Peter" it changes also the workContacts[0].name to Peter, but i want to be able to change the homeContacts without affecting the workContacts, how i can copy the values of homeContacts to WorkContacts and give them reference to new pointers and not share the same;

Thank you in advance

Dimitrisst
  • 11
  • 3
  • that's plain java, unrelated to javafx - and looks like working through a tutorial on language basics might be a good idea :) – kleopatra Sep 06 '20 at 09:41
  • this problem is very basic :/ you should initialized all array first same as this `Contact[] homeContacts = new Contact[10]; for( int i = 0 ; i < homeContacts.length ; ++i){ homeContacts[i] = new Contact(); }` – Morteza Jalambadani Sep 06 '20 at 09:44
  • Also, maybe you have `String` member variables, not `char` – msmilkshake Sep 06 '20 at 09:47
  • https://stackoverflow.com/a/12020435/11534880 – msmilkshake Sep 06 '20 at 09:49
  • Yes ,thank you i wrote this as example, the variables are String an i have created the homeContacts[i] = new Contact(); before the workContacts=homeContacts; but i get this error – Dimitrisst Sep 06 '20 at 09:52

1 Answers1

0

You write:

and i copy the values of homeContacts to WorkContacts massively like this

workContacts=homeContacts;

This is not a massive copy... this is just creating another reference workContacts that points to the same objects as the homeContacts list. You would have to create all new objects for you to be able to change one list without it affecting the other.

In your Contact object, you could create an overloaded constructor which takes another Contact as a parameter, which would then copy your three attributes over to the newly created object. Then you can properly copy the list over with:

for (int i=0; i<10; i++) {
  workContacts[i] = new Contact(homeContacts[i]);
}

and then you can make edits to one without affecting the other.

Similarly, at the time that you populate your values into your homeContacts, you could be doing the exact same thing to your workContacts so you populate two lists at the same time.

JimN
  • 340
  • 2
  • 10