This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

copying arrarys into a different class with methods

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant that worked a treat, many thanks
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ December 19, 2003: Message edited by: Alan Shiers ]
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!