CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    52

    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;
          }
       
       }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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 &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Aug 2009
    Posts
    52

    Re: simple array problem

    Quote Originally Posted by dlorde View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured