• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting int[] to double[]  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to code an array that holds the minimum, maximum, sum, product, and average of whatever number I input. I feel like I got the code for those 5 elements correct, and put them into an array. My problem, is when I compile it it tells me it's incompatible types: double[] cannot be converted to int[]. Now i have other functions that use int[] a, but I thought because I made my stats1 array a double it would convert over. I input the 1.0, 2.0, 3.0, 4.0 as test numbers to see if my code would do its job.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declaration on line 11 says show() takes an int[] but when you call it with the result of the stats() method, you're asking it to take a double[]
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sak wrote:I am trying to code an array that holds the minimum, maximum, sum, product, and average of whatever number I input.

Just FYI, version 8 allows you to do almost all of this with streams and built-in methods, and it even includes an IntSummaryStatistics (←click) class which includes everything you want except for your 'product' function - which you can calculate with the peek() method.

Just to give you a taster:
HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . Just FYI, version 8 allows you to do almost all of this with streams and built-in methods, . . .
If you simply want to convert the arrays from one type to another, try this:-Start reading about Streams in the documentation. Note that both code blocks shown start by creating an IntStream. If you need a product, try getting the summary statistics and multiplying the average by the count.You will get the usual problems about floating‑point imprecision.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!