Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setDouble () insanity

 
Dave Chin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15439
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why aren't you just doing:

data[k] = v;
 
Dave Chin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Chin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff.

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

Cheers,

Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic