Returning a multidimensional array.

B

Ben

Hi I am wondering if anyone could heko me with the following problem.

I have a method that contains a 2 dimensional array. I want to return
it but cannot get it to work. What line of code would i use to do
this.

e.g. return ########;


Thanks.

Ben
 
C

Christophe Vanfleteren

Ben said:
Hi I am wondering if anyone could heko me with the following problem.

I have a method that contains a 2 dimensional array. I want to return
it but cannot get it to work. What line of code would i use to do
this.

e.g. return ########;


Thanks.

Ben

The same as you would any other kind of object:

declare that the method returns that type, eg

public MyClass[][] someMethod() {
...
return some2dArrayOfMyclass;
}
 
P

Peter Ashford

Ben said:
Hi I am wondering if anyone could heko me with the following problem.

I have a method that contains a 2 dimensional array. I want to return
it but cannot get it to work. What line of code would i use to do
this.

e.g. return ########;


Thanks.

Ben

public int[][] myFunc(){
int[][] bob = new int[10][10];
return bob;
}
 
B

Ben

Thanks for your answer but I have tried that and keep get the message
"cannot resolve symbol - variable InitialisationValues. The code is
below and I am wondering if anyone can tell me where im going wrong.

Thanks

Ben

import java.io.*;

public class Initialisation
{

private int x;
private int y;
private int occurances;
private int where;

public Initialisation()
{
occurances = 6;
x = 0;
y = 0;
}

public String[][] getContents()
{


File aFile = new File("setup.txt");

int arraySize = 0,
counter = 0;

//...checks on aFile are elided
StringBuffer contents = new StringBuffer();

//declared here only to make visible to finally clause
BufferedReader input = null;
try
{
//use buffering
//this implementation reads one line at a time
input = new BufferedReader( new FileReader(aFile) );

String line = null; //not declared within while loop

while (( line = input.readLine()) != null)
{
arraySize = arraySize + 1;
}


String [] initialisationLines = new String [arraySize];


input = new BufferedReader( new FileReader(aFile) );

//While loop that fills the first array with a line form the
initialisation file.
while (( line = input.readLine()) != null)
{
initialisationLines[counter] = line;
counter = counter + 1;

}
String thing = null;

//Defining the array to hold commands
String[][] initialisationValues = new String
[arraySize][7];


for(x = 0; x < arraySize; x++)
{

thing = initialisationLines[x];

for(y = 0;y < 6; y++)
{

//Putting command into array.
where = thing.indexOf(",");

//Putting value into array
initialisationValues[x][y] =
thing.substring(0,where);

//Removes the command that has been put in to the
array from the entered string.
thing = thing.substring(where+1);
}
//Deals with the last command.
initialisationValues[x][6] = thing;
}
}

catch (FileNotFoundException ex)
{
ex.printStackTrace();
}

catch (IOException ex)
{
ex.printStackTrace();
}

finally
{
try
{
if (input!= null)
{
//flush and close both "input" and its underlying
FileReader
input.close();
}
}

catch (IOException ex)
{
ex.printStackTrace();
}
}

return initialisationValues;
}

public void run()
{
// put your code here
//return x + y;
}

public void getIntialisationData()
{
// put your code here
//return x + y;
}
}
 
S

Sudsy

Ben said:
Thanks for your answer but I have tried that and keep get the message
"cannot resolve symbol - variable InitialisationValues. The code is
below and I am wondering if anyone can tell me where im going wrong.

You declare your initialisationValues INSIDE the try block. It's
already out of scope by the time you get to the return statement.
Try defining it at the beginning of the method and assigning null
to it so the compiler doesn't complain.
 
B

Bjorn Abelli

...
Thanks for your answer but I have tried that
and keep get the message "cannot resolve symbol
- variable InitialisationValues. The code is
below and I am wondering if anyone can tell me
where im going wrong.

The error message tells the answer. At the point when you try to return
"initialisationValues" it doesn't exist, since it simply has gone "out of
scope".

I have snipped your example a bit to show what's happening:
public String[][] getContents()
{
[snip]

try
{

// And here you define "initialisationValues"
// *inside* the try-block...
String[][] initialisationValues =
new String[arraySize][7];

[snip]

// And here you *leave* the try-block...
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{ [snip]
}

// And hence "initialisationValues" have "disappeared"...

return initialisationValues;
}

You could declare initialisationValues in the beginning of getContents, like
this:

String[][] initialisationValues = null;

I haven't checked your code for any logical errors, but with this change it
at least goes through compilation.


// Bjorn A
 
B

Ben

Thanks! That works.

However I seem to have a new problem. I am trying to create a copy of
the array in the class it is returned to however when i call the
method. (i.e. Initialisation.getComments()) it says "non-static method
getContents() cannot be referenced from a static context."

I have tried making it static and made different alterations to the
code however i cannot get it to work. Does anyone know how to get it
working!!

Thanks!

Ben
 
C

Christophe Vanfleteren

Ben said:
Thanks! That works.

However I seem to have a new problem. I am trying to create a copy of
the array in the class it is returned to however when i call the
method. (i.e. Initialisation.getComments()) it says "non-static method
getContents() cannot be referenced from a static context."

I have tried making it static and made different alterations to the
code however i cannot get it to work. Does anyone know how to get it
working!!

Thanks!

Ben

Look at the "non-static can't be referenced" entry in the list of
errormessages at http://mindprod.com/jgloss/errormessages.html
 
E

Eki Y. Baskoro

G'Day,

The signature of your method depends on the type of your multidimensional
array, eg. String. For example, to return a two dimensional array of String
from a method called foo:

String [][] foo( /* arg signature */ ) {
String [][] result;
/* method body */
return result;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top