Forums Register Login

copying arrarys into a different class with methods

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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;
}
}
+Pie Number of slices to send: Send
Brilliant that worked a treat, many thanks
+Pie Number of slices to send: Send

[ December 19, 2003: Message edited by: Alan Shiers ]
+Pie Number of slices to send: Send
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.
This parrot is no more. It has ceased to be. Now it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1123 times.
Similar Threads
summing each row in a 2-dimensional array
Arrays with files
Two dimensional array total and sort
Sudoku Solver
double array file cleared up
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:23:39.