Results 1 to 11 of 11

Thread: Array

  1. #1
    DaoK
    Guest

    Wink Array

    Code:
    package arrayMainFrm;
    
    public class SimpleArray
    {
      public static void main ( String Args[] )
      {
        int myArray[] = {1,6,245,72,4,6,1,1,1};
        myArray = checkA(myArray[]);
    
        for ( int i = 0;i<myArray.length;i++)
          System.out.println(myArray[i]);
      }
    
      public static int checkA(int newArray[])
      {
        for ( int i = 0;i<newArray.length;i++)
          newArray[i]=newArray[i]*2;
      }
    }
    Red line = illegal start of expression

  2. #2
    You're trying to assign an integer to an array of integers.

  3. #3
    DaoK
    Guest
    ok but the blue is what I think I did not put but it still do not work
    Code:
    package arrayMainFrm;
    
    public class SimpleArray
    {
      public static void main ( String Args[] )
      {
        int myArray[] = {1,6,245,72,4,6,1,1,1};
        myArray = checkA(myArray[]);
    
        for ( int i = 0;i<myArray.length;i++)
          System.out.println(myArray[i]);
      }
    
      public static int[] checkA(int newArray[])
      {
        for ( int i = 0;i<newArray.length;i++)
          newArray[i]=newArray[i]*2;
      }
    }

  4. #4
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Once an array is defined like myArray, you should never use the syntax 'myArray[]'. myArray[i] makes sense, but not myArray[].

    So to answer your question, you might solve all your problems by saying:
    myArray = checkA(myArray);

    However, even that is a little scary because myArray on the left and myArray on the right point to the exact same location in memory. It may work out just fine, but be on the cautious side when debugging your results.

    cudabean

  5. #5
    DaoK
    Guest
    I do not see if you have took a look to the code but I just wanted to try to double the value of each value of the array by using a Function... Is that the good way with Java ?

  6. #6
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Well nevermind. I *was* thinking that instead of:

    myArray = checkA(myArray[]);

    I'd create:

    int[] outArray = checkA(myArray[]);
    myArray = outArray;

    But upon reflection I decided that that is a dumb idea.

    cudabean

  7. #7
    DaoK
    Guest
    Code:
      public static void main ( String Args[])
      {
        int myArray[] = {1,6,245,72,4,6,1,1,1};
        int myArray[] = new checkA(myArray[]);
    
        for ( int i = 0;i<myArray.length;i++)
          System.out.println(myArray[i]);
      }
    
      public static int[] checkA(int newArray[])
      {
        for ( int i = 0;i<newArray.length;i++)
          newArray[i]=newArray[i]*2;
      }
    Ok anyone can tell me what is not good now ?

  8. #8
    Code:
    int myArray[] = new checkA(myArray[]);
    checkA is not a class, you can't instantiate it like it is one.

  9. #9
    DaoK
    Guest
    Do you have the solution to it ? Because I tried a lots of thing and I have all the time an error

  10. #10
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Just remove the 'new'. Didn't this, as I had posted above, work?

    myArray = checkA(myArray);

    cudabean

  11. #11
    DaoK
    Guest
    Thx you all, thx Cudabean that worked but I changed something to return:

    Code:
    package DoubleArrPrjMain;
    
    public class ArrayFunction {
      public static void main ( String Args[] )
      {
        int myArray[] = {1,6,245,72,4,6,1,1,1};
        myArray = checkA(myArray);
    
        for ( int i = 0;i<myArray.length;i++)
          System.out.println(myArray[i]);
      }
    
      public static int[] checkA(int newArray[])
      {
        for ( int i = 0;i<newArray.length;i++)
          newArray[i]=newArray[i]*2;
          return newArray;
      }
    }

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