Results 1 to 4 of 4
Thread: Returning Array from a method
- 04-04-2012, 10:23 AM #1
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:
- 04-04-2012, 10:37 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 04-04-2012, 11:30 AM #3
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;
}We Learn Through Mistakes..,
Manfizy:rolleyes:
- 04-04-2012, 12:01 PM #4
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.Also, why is personArray a static member?Java Code:personArray = new String[][] {{..}...}
It's not a constant.i use the array constant "personArray"
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Returning get() method in put() method in a Hash Map
By Wpbops in forum New To JavaReplies: 1Last Post: 12-15-2011, 04:02 AM -
My method keeps returning 0
By ToolJob in forum New To JavaReplies: 11Last Post: 03-27-2011, 05:22 PM -
Returning Value from a method
By Mirix in forum New To JavaReplies: 12Last Post: 06-01-2010, 09:48 PM -
Inherited method returning bad value
By viking90 in forum New To JavaReplies: 11Last Post: 04-07-2010, 03:53 PM -
Need help. Method won't returning proper value..
By zlwilly in forum New To JavaReplies: 2Last Post: 12-02-2008, 09:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks