Results 1 to 4 of 4
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default Returning Array from a method

    Hello Programmers,

    I have a method that populates an array and returns it.
    I need two methods, one that populates the array and another one that shows the contents of the array. Both methods should be triggered from the main method.
    I have done exactly that but i get a null when i try to display contents of the array from the main method.
    Someone please check my code and tell me where i'm making a mistakes.

    Java Code:
    public class ArrayExample {
    
    	public static String[][] personArray;
    	public ArrayExample() {
    		super();
    	}
    	/**
         * Demonstates initializing and populating a multi-dimensional array in
         * several statements
         */
        public String[][] multiDimensionalArray() {
    
           	String[][] personArray =  
           		 {
    			  {"John","Phillips","DOB"},
    			  {"Manfred","Hahn","DOB"},
    			  {"Jane","Dickson","DOB"}
           		 };
    
    	return personArray;
    
         }
      /**
        *
        */	 
         
       public void showArrayContent(String[][] ar){
    	//multiDimensionalArray();        
    	int i,j;
    	for(i=0; i< ar.length; i++){
    
    		for(j=0; j<ar[i].length; j++){
    		
    		 System.out.println( "multiDimensionalArray()"+i+". "+ ar[i][j]);
    		 }
    	}
         		
      }
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ArrayExample arrays = new ArrayExample();
            arrays.multiDimensionalArray();
            //System.out.println("ARRAY OUT : "+personArray[1][1]);
            arrays.showArrayContent(personArray);
    	
    
    	}
    
    }
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Returning Array from a method

    In the multiDimensionalArray() method you are creating an array local to that method, not the one that is an attribute of the class.
    The attribute array 'personArray' is always null.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default Re: Returning Array from a method

    I know that BUT if i use the array constant "personArray" i get the error "Array constants can only be used in initializers"

    public String[][] multiDimensionalArray() {

    personArray =
    {
    {"John","Phillips","DOB"},
    {"Manfred","Hahn","DOB"},
    {"Jane","Dickson","DOB"}
    };

    return personArray;

    }
    Quote Originally Posted by Tolls View Post
    In the multiDimensionalArray() method you are creating an array local to that method, not the one that is an attribute of the class.
    The attribute array 'personArray' is always null.
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Returning Array from a method

    Use code tags, not quote tags for posting code.

    Assignment to an array literal in the way you have tried is only valid in conjunction with array declaration. For assigning a previously declared array, the new keyword is required.
    Java Code:
    personArray = new String[][] {{..}...}
    Also, why is personArray a static member?

    i use the array constant "personArray"
    It's not a constant.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 12-15-2011, 05:02 AM
  2. My method keeps returning 0
    By ToolJob in forum New To Java
    Replies: 11
    Last Post: 03-27-2011, 06:22 PM
  3. Returning Value from a method
    By Mirix in forum New To Java
    Replies: 12
    Last Post: 06-01-2010, 10:48 PM
  4. Inherited method returning bad value
    By viking90 in forum New To Java
    Replies: 11
    Last Post: 04-07-2010, 04:53 PM
  5. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 10:44 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
  •