• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a new constructor - arrays  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I want to write a constructor that creates a new object with the data from the array values. I don't know where to start. It's the last method in the code:


 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you are trying to get a particular value from an array by using its index, there is a much simpler way to write that get method. It will also execute faster.It is potentially dangerous to copy an array into an object, because you have two references to the same array and code outside the class can change the state of the array. Remember arrays are always mutable.You can sort that out for arrays of primitives or immutable types like thisBut you should consider why you want to copy arrays; there are often better ways to do things than copying arrays.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

It's a bit hard to give specific advise as we don't know what the class does or why it has 3 double arrays but basically you need to create an array of at least the same size as the array being passed in, copy everything from the passed in array to your new array and finally set the value of n to the number of values you have copied.

If we give us more information on what you are trying to achieve we may be able to give better advice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!