Touchstone57
02-23-2009, 04:22 PM
Basically, I am have been trying to use the System.arraycopy method to copy the elements of one array to another, and using a for loop to print out the elements, but I cannot get it to successfully copy the elements of one array to another.
The problem stems I think from my use of two dimensional arrays, because in the parameters for System.arraycopy the source position and destination position can only specify a single value, whereas I am using 2D arrays, which will hold two values, but I'm not sure.
What can I do?
Here is a little idea of the array I have used, where I create a 2D array, and store library items and pass values in using the constructors.
LibraryItem[][] stItems = new LibraryItem[30][30];
//make items for the library
//For books prices £10 - £20
stItems[0][0] = new shortterm.Book(staff.getID(), "Java Now!", "Kris Jamsa", "J amsa Press", 1996,
"1-884-13330-4", 16.95);
stItems[0][1] = new shortterm.Book(staff.getID(), "Java in a Nutshell: A Desktop Quick Reference for Java Programmers",
"David Flanagan", "O'Reilly & Associates Inc.", 1996,
"1-565-92183-6", 19.95);
And here, it's meant to copy the array stItems to refItems, and then using a for loop print out object attributes.
System.arraycopy(stItems, 0, refItems, 5, 10);
System.out.println('\n' +
"Price" + " " + " Book Title" +
"\n-----------------------------------------------------------" +
"-------------------------------------------------------------" +
"-----------------------");
for(int i = 0; i < refItems.length; i++){
for(int n = 0; n < refItems.length; n++){
if(refItems[i][n] instanceof reference.Book){
System.out.println("£" + ((reference.Book)refItems[i][n]).getPrice() + " " + refItems[i][n].getTitle() + " by " + ((reference.Book)refItems[i][n]).getAuthor() + ", " +
((reference.Book)refItems[i][n]).getPublisher() + ", " + ((reference.Book)refItems[i][n]).getYearPublished()
+ ", " + ((reference.Book)refItems[i][n]).getISBN() + ".");
}
}
}
The problem stems I think from my use of two dimensional arrays, because in the parameters for System.arraycopy the source position and destination position can only specify a single value, whereas I am using 2D arrays, which will hold two values, but I'm not sure.
What can I do?
Here is a little idea of the array I have used, where I create a 2D array, and store library items and pass values in using the constructors.
LibraryItem[][] stItems = new LibraryItem[30][30];
//make items for the library
//For books prices £10 - £20
stItems[0][0] = new shortterm.Book(staff.getID(), "Java Now!", "Kris Jamsa", "J amsa Press", 1996,
"1-884-13330-4", 16.95);
stItems[0][1] = new shortterm.Book(staff.getID(), "Java in a Nutshell: A Desktop Quick Reference for Java Programmers",
"David Flanagan", "O'Reilly & Associates Inc.", 1996,
"1-565-92183-6", 19.95);
And here, it's meant to copy the array stItems to refItems, and then using a for loop print out object attributes.
System.arraycopy(stItems, 0, refItems, 5, 10);
System.out.println('\n' +
"Price" + " " + " Book Title" +
"\n-----------------------------------------------------------" +
"-------------------------------------------------------------" +
"-----------------------");
for(int i = 0; i < refItems.length; i++){
for(int n = 0; n < refItems.length; n++){
if(refItems[i][n] instanceof reference.Book){
System.out.println("£" + ((reference.Book)refItems[i][n]).getPrice() + " " + refItems[i][n].getTitle() + " by " + ((reference.Book)refItems[i][n]).getAuthor() + ", " +
((reference.Book)refItems[i][n]).getPublisher() + ", " + ((reference.Book)refItems[i][n]).getYearPublished()
+ ", " + ((reference.Book)refItems[i][n]).getISBN() + ".");
}
}
}