Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to declare an array accesable by multiple methods in the same clas 2

Status
Not open for further replies.

gmarrufo

IS-IT--Management
Apr 16, 2003
26
US
Hi,

I'm brand new to Java and I created a little app. I have an array (two dimensional) that is populated by a file. That part at least is working I can see the array been populated but How do I get this array to be visible for any other method in the same class?

The method that populates the array is:

private static void readData()
{
iterates thru file and populates array
array name is: strRecord [][]
}

I can get some more code if needed to get a better picture of what I'm doing.

Thanks in advance for any help on this matter :)

G
 
to make the array visible in the class only, use private modifier.


public class SampleArray{

private int a[] = {1,2,3};

private int readArray(){

for (int i = 0, i < a.length; i++){
System.out.println(&quot;value is <&quot; + a );
}
}

private int setArray(){

//some serious coding here
}

}

~za~
You can't bring back a dead thread!
 
class myApp
{
private final int maxRow = 1000, maxCol = 80;
private char strRecord[][] = new char [maxRow][maxCol];
private int lastRow = 0;
public myApp()
{
for (int y=0; y<maxRow; y++)
{
for (int x=0; x<maxCol; x++)
strRecord[y][x] = 0;
}
}
private void readData(String test)
{
int testLen = test.length();
for (int i=0; i<testLen; i++)
strRecord[0] = test.charAt(i);
lastRow = 0;
}
private void printOutput()
{
for (int i=0; i<=lastRow; i++)
{
System.out.println(&quot;&quot;);
for (int k=0; k<maxCol; k++)
System.out.print(strRecord[k]);
}
System.out.println(&quot;&quot;);
}
public static void main(String args[])
{
myApp myAppObj = new myApp();
myAppObj.readData(&quot;This is text.&quot;);
myAppObj.printOutput();
}
}
You may remove the static modifier in your program except
public static void main(String args)
Please give a credit to me and the above replier if you find our answers are useful.
 
Thank you both of you guys. You are Awesome!!

G
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top