File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes copying arrarys into a different class with methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "copying arrarys into a different class with methods" Watch "copying arrarys into a different class with methods" New topic
Author

copying arrarys into a different class with methods

bob spencer
Greenhorn

Joined: Apr 10, 2003
Posts: 5
Hey all, another post from a greenhorn
i have the following program working but i'd like to send the array to another class, so that i can make some methods to manipulate the array
can some one tell me how to do it please. at the very bottom is a failed attempt.
public class Trial
{
public static void main (String[] args)
{
int [][] table = new int [7][7]; //initialises array
//---------------sets all values in array to -1---------
for (int row=0; row < table.length; row++)
{
for (int col=0; col<table[row].length; col++)
{ table[row][col] = -1;
}
}
//--------------input values into array-------------------
table[1][2]=2;
table[1][6]=3;
table[2][1]=2;
table[2][3]=5;
table[3][2]=5;
table[3][4]=9;
table[4][3]=9;
table[4][5]=1;
table[5][4]=1;
table[5][6]=8;
table[6][1]=3;
table[6][5]=8;
/*-----------------Prints out array -----------------------
for (int row=0; row < table.length; row++)
{
for (int col=0; col < table[row].length; col++)
System.out.print (table[row][col] + "\t");
System.out.println();
}
*/
}//closes public method
}//closes class

//-----------------------------------------this is my failed attempt
public class Contacts
{
private int [][] newtable = new int [7][7];

public Contacts (int a[][])
{
newtable[][]=a[][];
}
public String toString()
{
return newtable ;
}
}
any help would be greatly appreciated
Thanks
B
Gillian Bladen-Clark
Greenhorn

Joined: Dec 13, 2003
Posts: 18
Try this. The output displayed via my toString() method was:
-1-1-1-1-1-1-1
-1-12-1-1-13
-12-15-1-1-1
-1-15-19-1-1
-1-1-19-11-1
-1-1-1-11-18
-13-1-1-18-1
toString() just needs to build a string representation of your object.
The constructor just makes your instance's array variable point to the same array passed in (aTable).
public class Trial {
public static void main(String[] args) {
int[][] table = new int[7][7]; //initialises array
//---------------sets all values in array to -1---------
for (int row = 0; row < table.length; row++) {
for (int col = 0; col < table[row].length; col++) {
table[row][col] = -1;
}
}
//--------------input values into array-------------------
table[1][2] = 2;
table[1][6] = 3;
table[2][1] = 2;
table[2][3] = 5;
table[3][2] = 5;
table[3][4] = 9;
table[4][3] = 9;
table[4][5] = 1;
table[5][4] = 1;
table[5][6] = 8;
table[6][1] = 3;
table[6][5] = 8;
Contacts myTable = new Contacts(table);
System.out.println(myTable);
} //closes public method
} //closes class
public class Contacts
{
private int [][] newtable = new int [7][7];

public String toString()
{String str = "";
for (int row = 0; row < newtable.length; row++) {
for (int col = 0; col < newtable[row].length; col++) {
str += "\t"+newtable[row][col];
}
str += "\n";
}
return str ;
}
public Contacts(int[][] aTable) {
this.newtable = aTable;
}
}
bob spencer
Greenhorn

Joined: Apr 10, 2003
Posts: 5
Brilliant that worked a treat, many thanks
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237

[ December 19, 2003: Message edited by: Alan Shiers ]
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
isn't there a static class called Arrays designed for throwing around array data.
I've never used it but I am sure it's in there somewhere.


"....bigmouth strikes again, and I've got no right to take my place with the human race...."<p>SCJP 1.4
 
jQuery in Action, 2nd edition
 
subject: copying arrarys into a different class with methods