Results 1 to 5 of 5
  1. #1
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Is there a way to pass an array from an instance of a class?

    This is probably undoable, but I want to be sure...

    The following code is just to try and get this idea to work,
    it has no redeeming value or other purpose.

    Java Code:
    public class Int_Test {
    
      public void arrayMat(int dimension) {
        
         int[] temp;
         temp = new int[dimension];
         
      for (int i=0; i< dimension; i++) {
          
          temp[i] = i*2;
          System.out.println("temp = " + temp[i]);
        
      } // end of for
       
       return temp[];
           
      }
        
      public static void main(String[] args) {
      
        int dimension = 10;
        int[] iArray = new int[dimension];
        Int_Test expt = new Int_Test();
        iArray[] = expt.arrayMat(dimension); 
            
        for ( i = 0 ; i < 10 ; i++ ) {
          
           System.out.println("i = " + i );
           System.out.println("Temp[i] = " + iArray[i]);
          
        } // end of for
        
      }
    }
    I'm not sure my syntax is even correct. I get the following errors:

    Java Code:
    Int_Test.java:15:17: error: '.class' expected
       return temp[];
                    ^
    Int_Test.java:25:11: error: not a statement
        iArray[] = expt.arrayMat(dimension); 
              ^
    Int_Test.java:25:13: error: ';' expected
        iArray[] = expt.arrayMat(dimension); 
                ^
    3 errors
    Passing one value is not an issue... but what section of the manual do I need to read in order to solve this?
    Or am I trying to glue feathers on a fish?

    Thanks...

    Mark

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is there a way to pass an array from an instance of a class?

    Just return temp. Not temp[].

    And instead of a return type of void, it should be int[] since temp is an int array.
    There may be other issues as I did not fully scrutinize your code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is there a way to pass an array from an instance of a class?

    And line 28 should be:

    Java Code:
    iArray = expt.arrayMat(dimension);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is there a way to pass an array from an instance of a class?

    And one more thing related to your topic. You can also do this:

    Java Code:
    int[] temp = new int[10];
    
    fillArray(temp);
    
    public void fillArray(int[] a) {
       for (int j = 0; j < a.length; a++) {
           a[j] = j*2;
       }
    }

    The above works since you are not changing the array but filling it using the array reference. Remember that arrays
    are objects so you can alter what the reference refers too but not the reference itself.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Is there a way to pass an array from an instance of a class?

    THe void was a left over at me throwing anything against the wall hoping it would stick.

    Thanks for the code... I never thought to drop the brackets... But the little nuggets were also very instructive...

    Really appreciate the help!

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2013, 04:19 PM
  2. Replies: 3
    Last Post: 12-15-2012, 04:46 AM
  3. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  4. Replies: 7
    Last Post: 02-11-2011, 06:38 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •