Results 1 to 10 of 10
  1. #1
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Question about returning array??

    I have an Array that I created inside of a try/catch block. I am trying to figure out how to return that array, because as of right now the array is local to the try/catch block and I can't access it.

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Question about returning array??

    Declare reference variable of Array object outside try/catch block.

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about returning array??

    I can't...I'm using a counter inside a for loop that also inside the try/catch block, and the size of the array is based off the counter. So if I declare array outside the size would have to be set manually.

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Question about returning array??

    If counter inside for loop is declared inside for loop, then counter is visible only inside for loop, not inside try/catch block.

    Say:
    Java Code:
    try {
    
    //counter is not visible
    
       for (int counter = 0; ;) {
       //counter is visible
       }
    
    //counter is not visible
    }
    catch (...) {
    //counter is not visible
    }
    
    //counter is not visible
    Last edited by diamonddragon; 01-27-2012 at 04:20 AM.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about returning array??

    Counter is declared in try/catch...just incremented to the correct amount in the for loop.

  6. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Question about returning array??

    So, what is counter having with Array reference variable?

  7. #7
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about returning array??

    I decided to just create a class so I can access class by the object. Real quick...if I have this in my main method

    Java Code:
    File scoresArray = readFile(fileName);
    Then in readFile I am returning:

    Java Code:
    return new File(scoresArray, sum, j);
    What do I need to do to main so that I can access the values I am returning, or is that all I need to do? Then I can say scoresArray.sum right???

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Question about returning array??

    If by accessing class You mean accessing class static members, than:
    You can only access non private static members of class from outside class, say: ClassInstance.nonPrivate_Static_Member
    But if static member is private, than You can only access it from inside that class, say: Class.Private_Static_Member

    You can use dot(.) operator to connect say reference variable to a method or to connect class name with one of its static fields.
    Last edited by diamonddragon; 01-27-2012 at 05:54 AM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Question about returning array??

    Quote Originally Posted by kraigballa View Post
    I have an Array that I created inside of a try/catch block. I am trying to figure out how to return that array, because as of right now the array is local to the try/catch block and I can't access it.
    You have one of two choices:
    • You can return the array from within the try block and throw an exception from the catch block (or return null after the try/catch blocks ... probably not a good idea)
    • Or you can declare the array before the try/catch block but initialize it within the try/catch block. This is probably the best option.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Question about returning array??

    Quote Originally Posted by kraigballa View Post
    I decided to just create a class so I can access class by the object. Real quick...if I have this in my main method

    Java Code:
    File scoresArray = readFile(fileName);
    Then in readFile I am returning:

    Java Code:
    return new File(scoresArray, sum, j);
    What do I need to do to main so that I can access the values I am returning, or is that all I need to do? Then I can say scoresArray.sum right???
    If you're creating a class for this, then you'll want to give that class "getter" (also known as accessor) methods to allow other classes to access the values held by objects of this class. You can have a getter method for an array just like you can for a primitive field such as an int. i.e.,

    Java Code:
    public class Foo {
    
      private int[] myArray = {1, 2, 3, 4, 5};
    
      public int[] getMyArray() {
        return myArray;
      }
    }

Similar Threads

  1. Returning Array Row
    By Zocheyado in forum New To Java
    Replies: 2
    Last Post: 04-15-2011, 02:23 AM
  2. returning double array
    By Billaguana in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 03:59 AM
  3. returning array
    By aizen92 in forum New To Java
    Replies: 4
    Last Post: 01-08-2011, 03:10 PM
  4. Returning An Array
    By elektronika in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:43 PM
  5. Returning array problem.
    By Chase in forum New To Java
    Replies: 4
    Last Post: 10-21-2008, 09:07 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
  •