The error is because you are trying to assign a double value to array in the following statement:
mylist = bob.nextDouble();
as mylist is defined as an array here:
double[] mylist;
First you need to intialize your array:
double[] mylist = new double[2]; // initialise array to have two elements
and then try to assign the array elements with the user input like this:
mylist[0] = bob.nextDouble();
mylist[1] = bob.nextDouble();
Answer from Juned Ahsan on Stack OverflowThe error is because you are trying to assign a double value to array in the following statement:
mylist = bob.nextDouble();
as mylist is defined as an array here:
double[] mylist;
First you need to intialize your array:
double[] mylist = new double[2]; // initialise array to have two elements
and then try to assign the array elements with the user input like this:
mylist[0] = bob.nextDouble();
mylist[1] = bob.nextDouble();
mylist is an array of doubles, not a single double.
When you do mylist = bob.nextDouble(); you are basically trying to make an entire array equal a single number.
You instead need to assign the number to a specific place in the array.
Initialize the array with the amount of elements you are going to store in the array:
double[] mylist = new double[2];Assign the double to a specific place in the array:
mylist[0] = bob.nextDouble(); mylist[1] = bob.nextDouble();
Videos
This is array initializer syntax, and it can only be used on the right-hand-side when declaring a variable of array type. Example:
int[] x = {1,2,3,4};
String[] y = {"a","b","c"};
If you're not on the RHS of a variable declaration, use an array constructor instead:
int[] x;
x = new int[]{1,2,3,4};
String[] y;
y = new String[]{"a","b","c"};
These declarations have the exact same effect: a new array is allocated and constructed with the specified contents.
In your case, it might actually be clearer (less repetitive, but a bit less concise) to specify the table programmatically:
double[][] m = new double[4][4];
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
m[i][j] = i*j;
}
}
You can initialize an array by writing actual values it holds in curly braces on the right hand side like:
String[] strArr = { "one", "two", "three"};
int[] numArr = { 1, 2, 3};
In the same manner two-dimensional array or array-of-arrays holds an array as a value, so:
String strArrayOfArrays = { {"a", "b", "c"}, {"one", "two", "three"} };
Your example shows exactly that
double m[][] = {
{0*0,1*0,2*0,3*0},
{0*1,1*1,2*1,3*1},
{0*2,1*2,2*2,3*2},
{0*3,1*3,2*3,3*3}
};
But also the multiplication of number will also be performed and its the same as:
double m[][] = { {0, 0, 0, 0}, {0, 1, 2, 3}, {0, 2, 4, 6}, {0, 3, 6, 9} };
An array is an object in java and therefore it is printed by calling its toString() method. This method is not very helpful for the array class and doesn't print the arrays contents. To print the array content you can call Arrays.toString(yourArray) which will return a string representation of the array contents.
You should replace
System.out.println(valueOfElements(number));
with
System.out.println(Arrays.toString(valueOfElements(number)));
and add the following import statement:
import java.util.Arrays;
First you have to understand that in java an array variable is a reference. This means that when you try an print out an array variable, it prints out a memory address, not elements of the array. The way to fix your issue is to save the return value of the function into the array, and then for loop through every element in the array.
double[] values = valueOfElements(number);
for(int i = 0; i < values.length; i++){
System.out.println(values[i]);
}
The [] dereferences the double, so you are accessing the double value, and not the memory address value.
Hi! I would appreciate some help with this exercise.
Prompt:
There is an array with some numbers. All numbers are equal except for one. Try to find it!
It’s guaranteed that array contains at least 3 numbers.
The tests contain some very huge arrays, so think about performance.
Example:Kata.findUniq(new double[]{ 1, 1, 1, 2, 1, 1 }); // => 2
My Code:
public class Kata {
public static double findUniq(double arr[])
{
for (int i = 0; i < arr.length; i++) {
for (int k = i + 1; k < arr.length; k++) {
if (arr[i] != arr[k]) {
return i;
}
}
}
else if
{ return; }
}
}
My Question:
How do i return the different double from the array? I know that only ints work for loops, so I'm not sure how to return a double for it.
Thank you