• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

setDouble () insanity

 
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 );
^
 
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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;
 
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
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic