|
-
November 22nd, 2009, 09:33 AM
#1
simple array problem
How do i print out the answer of v[i] *=v[i]; in squareVector in main method? i get [D@19821f
Code:
public class arrays2{
public static void main(String[] args){
double [] array1= {5,10,15,20};
for(int i=0; i < array1.length; i++){
System.out.println(squareVector(array1));
}
}//end main
public static double [] squareVector(double[]v){
for(int i =0; i <v.length; i ++)
v[i] *=v[i];
return v;
}
}
-
November 22nd, 2009, 10:20 AM
#2
Re: simple array problem
What exactly are you trying to print? In the code, you're squaring every element in the array in squareVector, then trying to print the whole array. In your question you're asking how to print the result of a single element being squared...
You can't just print a whole array by passing it to println, you have to print each element separately.
A prudent question is one-half of wisdom...
F. Bacon
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 22nd, 2009, 10:24 AM
#3
Re: simple array problem
 Originally Posted by dlorde
What exactly are you trying to print? In the code, you're squaring every element in the array in squareVector, then trying to print the whole array. In your question you're asking how to print the result of a single element being squared...
You can't just print a whole array by passing it to println, you have to print each element separately.
A prudent question is one-half of wisdom...
F. Bacon
Yes how do i print them seperately? im trying to print the returned value v
for(int i=0; i < array1.length; i++){
System.out.println(squareVector(array1[i]));
}
Error
arrays2.java:6: squareVector(double[]) in arrays2 cannot be applied to (double)
System.out.println(squareVector(array1[i]));
Last edited by hugo84; November 22nd, 2009 at 10:26 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|