[Logo]
Forums Register Login
Double[] and Addition?


Trying to just apply a function toDo to all the elements in the array, why + operator broke for double[]?
Why would you expect the + operator to be applicable to an array?
You add numbers, not arrays of numbers.
What is an Operation? You can pass pointers to functions and operations in C, but you cannot in Java. Please explain exactly what you are trying to do, then we can work out whether it is feasible at all.
Basically I'm trying to apply an operation to every element of the array. For example, if the array is {2.0, 3.0, 4.0} and the operation consisted of adding 10, the resulting array would {12.0, 13.0, 14.0}. If I can't use the + operator on array, I need to extract the element 1st and then do the operation then slam it back into an array. So making a temp double list then converting it back into a array?


By the way, your variable names are less than self-documenting. Reminds me of a guy I worked with in the 80's that named all his variables X. X, XX, XXX, XXXX, etc...
 

Robert D. Smith wrote:

By the way, your variable names are less than self-documenting. Reminds me of a guy I worked with in the 80's that named all his variables X. X, XX, XXX, XXXX, etc...



Yeah, that's basically what I'm trying to do, however, java tells me "The operator += is undefined for the argument type(s) double, Operation".
Yes, the += operator will only work if its right operand is a number. The concept of making a function into an object is alien to Java. You might be able to do it in Java8 with a λ‑expression. The nearest I can think of is something like this:-If you look up pass‑by‑value and pass‑by‑reference, you find that Java only supports pass‑by‑value, but all arrays are mutable reference types, so changes applied inside the method alter the state of the original array. Another example of a method which does that:- this one.
 

William Nerem wrote:Trying to just apply a function toDo to all the elements in the array, why + operator broke for double[]?


I think everybody's pretty much explained that, but my question to you is: why did you think that '+' would be correct in the first place?

Even without the more generic lambdas, you could have created something that worked for any array of number Objects with a bit of generics, viz:and then your code might look something like:and I suspect you could make it even simpler by creating a utility class for numeric arrays.

HIH

Winston
 

Winston Gutkowski wrote:

William Nerem wrote:Trying to just apply a function toDo to all the elements in the array, why + operator broke for double[]?


I think everybody's pretty much explained that, but my question to you is: why did you think that '+' would be correct in the first place?

HIH

Winston



Short answer, I'm new to java. Learning about the basics and using loops encourages that type of mentality when using operators. It's only natural, that, when using those same loops for basic functions that I would use it for Arrays as well. Being new to Arrays, I find myself stumbling on issues of "This is an object.", and you have to really bend-over backwards to make things happen.

Logically if I'm going through each array element via length, I should then be able to said operation to each element as it's looping through it's length. Makes sense, but again, Java is very complicated and you have to syntax your way to victory. I have literally only spent a couple of months using java, hopefully this can sum up the reasonings for such a "why did you think that '+' would be correct in the first place?" question.
 

William Nerem wrote:Short answer, I'm new to java...


Fair enough.

Oddly enough, the idea of a generic function that you simply "apply" to all elements in an array or collection is VERY Object-Oriented, so you're definitely thinking along good lines; it's just the implementation that needs a bit of work - but that'll come with time and practise.

Good luck.

Winston

This thread has been viewed 820 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 15, 2018 04:45:52.