PDA

View Full Version : arrayCopy method and 2D arrays


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() + ".");
}
}

}

Old Pedant
02-23-2009, 08:55 PM
You can't use System.arrayCopy to copy a 2D array.

You need to understand that Java doesn't *REALLY* HAVE 2D arrays. It really only has arrays-of-arrays. And when you use arrayCopy, it only "sees" the "outer" array.

It copies element by element, so your destination array ends up using the *SAME INNER ARRAY OBJECTS* as the source array!

And, no, you 2D array (as you declared it) does not hold "two values" (I assume you meant "per element"). It holds *30* inner values per each of the 30 outer elements...in other words 900 elements total.

Note that in the code you showed, you never changed any elements of the outer array. Instead, you just assigned to elements of inner array contained by the FIRST element of the outer array.

You have shown nothing in that code, so far, that indicates to me any reason for using a 2D array. What, for example, do you store in element [1][0] ???

Touchstone57
02-23-2009, 09:59 PM
Well, basically the books stored in the array have different price ranges, so different rows of the array take different price ranges, so row 0 being £10 - £20, row 1 £20 - 30, the books within those price ranges stored in the columns.

I'm not too interested as to the size of the array, I just want to get the desired output, which would be to add (copy) stItems to refItems, then print them off, but so far no luck.

I tried using nested for loop here, so that every element in the refItems array would be assigned with one from stItems, but it doesn't appear to work. Any ideas? Thanks.


for(int i = 0; i < stItems.length; i++){
for(int n = 0; n < stItems.length; n++){
refItems[i][n] = stItems[i][n];
}
}

Old Pedant
02-24-2009, 12:39 AM
Close.


for(int i = 0; i < stItems.length; i++){
for(int n = 0; n < stItems[ i ].length; n++){
refItems[i][n] = stItems[i][n];
}
}


I think that should work.

Understand that in Java there is no rule that says that every element of an outer array has to contain the same number of inner array elements.

That is, you *can* create a "triangular" array (just as an example) such as:

Object[ ] foo = new Object[20];
for ( int i = 0; i < 20; ++i )
{
foo[ i ] = new int[ i+1 ];
}

So you'd have "rows" in the outer array each of which contains an array of int's, but the number of ints would depend on WHICH row.