aspose file tools*
The moose likes Beginning Java and the fly likes setDouble () insanity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "setDouble () insanity" Watch "setDouble () insanity" New topic
Author

setDouble () insanity

Dave Chin
Greenhorn

Joined: Sep 25, 2005
Posts: 5
Hi All,

I cant get this mutator fxn to work and its driving me crazy. Can someone help me with this?? data is defined in my class as an array and Im trying to change a double at index k to a new double defined as v. setDouble is defined in the class Arrays.

Many thanks in advance.

Code:

//Set the data value at index k to v. (Mutator fxn)
public void setDataValue(int k, double v) {

System.out.print ("Changing data " + "[");
System.out.println (k + "] " + "to " + v );

Arrays.setDouble ( data, k, v );


Error Message:

cannot find symbol
symbol : method setDouble(double[],int,double)
location: class java.util.Arrays
Arrays.setDouble ( data, k, v );
^
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Hi Dave
That method really doesn't exist in Arrays, you may be thinking of Array?

setDouble(Object, int, double) - Static method in class java.lang.reflect.Array
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

Why aren't you just doing:

data[k] = v;


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Dave Chin
Greenhorn

Joined: Sep 25, 2005
Posts: 5
Hi Ta Ri Ki,

Thank you for your response. I tried Array.setDouble () as well but got the similar array. Forgive my ignorance, but since the method is a submethod of java.lang, will I be able to call it this way? If not, how should the code be written.

Jesper,

I agree that would be the more simple way to do this, but Im trying to practice writing mutator fxns.

Many thanks again,

Dave
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Dave Chin:
...since the method is a submethod of java.lang, will I be able to call it this way? ...

This Array class is not in the automatically imported package "java.lang" -- it's actually in "java.lang.reflect." So you should use the following import statement...

import java.lang.reflect.Array;


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Dave Chin:
H
I agree that would be the more simple way to do this, but Im trying to practice writing mutator fxns.


Practicing writing mutators is one thing but using java.lang.reflect.Array when you
could write data[k]=v in your mutator is another thing!

That reminds me of a gesture used by a developer I knew. He said
good code does this -- and then he would tug on his left ear lobe
with his right hand, in the obvious way -- but poorly written code does
this -- and then he would pass his right arm over the top of his head and
wrap his right hand around to tug on the left lobe again. Once we learned
the significance of all this, he would just silently make the convoluted
gesture to indicate his scorn of tortured code.


There is no emoticon for what I am feeling!
Dave Chin
Greenhorn

Joined: Sep 25, 2005
Posts: 5
Thanks Jeff.

You are right. This would be more straight forward. Lesson learned.

Cheers,

Dave
 
 
subject: setDouble () insanity