aspose file tools*
The moose likes Java in General and the fly likes Array Reference Problem.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Array Reference Problem...." Watch "Array Reference Problem...." New topic
Author

Array Reference Problem....

Shaun Brierly
Greenhorn

Joined: Aug 22, 2001
Posts: 17
I'm not sure if Sun considers this a "feature" or what, but i'm having trouble with an array in a while loop, the Array gets changed each time though the loop, however the way Java references objects, the Array's reference never changes, and when I try to pass the Array to get processed, it sends the same reference, which contains the orignal data of the 1st iteration through the loop, however when I System.out.println( array[0] ); you can see the data is changing... any work arounds to this? This is a pretty rediculous bug, or whatever, sounds intentional...
Thanks,
Shaun Brierly
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
It is intentional. For example:

Cheers, Neil
Shaun Brierly
Greenhorn

Joined: Aug 22, 2001
Posts: 17
Thanks for reply however, if I clone my array everytime it runs though a loop the computer it's being run on would be crippled... the Array is an array of bytes being read in chunks of 4196 bytes and sent through a socket, That would be alot of clones for large files!
Shaun Brierly
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Perhaps you could post an example of the loop code, to see if there is a non-intrusive way of performing the same job? Just in case you haven't posted before, if you wrap your code in [code] and [/code], the indentation will remain intact
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Here's a a common example, copying bytes from an InputStream to an OutputStream:

There's only one array here, created once, before the loop begins. It just gets continuously re-used. Every time read() is called, it writes new data into the array, overwriting anything already there. But that's OK because each time through the loop, we process everything that was read that time, so we don't need to remember it any more.
Does that help?
[ October 10, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Shaun Brierly
Greenhorn

Joined: Aug 22, 2001
Posts: 17
Hi, thanks for replies, yes that's exactly what I have, however for some reason the buffer is never updated... I read a post in Google Groups that it's a problem where the array itself is an object and when you pass it as the array it keeps the same reference to it or something, so it doesn't get updated or something. When I do System.out.println(array);
System.out.println(array[0]);
the first always prints out the same thing
while the 2nd line always prints out a different value. Yes, I know, very weird, which is why i'm trying to figure out why it's doing this...
Thanks,
Shaun
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
An array is just a set of references to other things.
Think of it as a rope with a bunch of buckets hanging from it.
When you change one thing, like:
array[0] = somethingNew;
you are just dumping out one bucket (the first one, with the big red zero painted on it) and putting something new in it. The rope and buckets are still the same - it's just the contents of one bucket that has changed.
You are only changing the contents of one element of array, array[0], but you have not changed array at all.
This is a good thing. If everytime you wanted to put something in a bucket you had to throw away the rope and all the buckets, get a new rope and tie on new buckets, then refill all the new buckets with the stuff that fell out when you took down the old rope - you'd never get around to whatever it was you wanted to do with all that rope and those buckets
Shaun Brierly
Greenhorn

Joined: Aug 22, 2001
Posts: 17
Thanks for replies, I used the functions you described, and my orignal problem still remains... the same information is being passed over and over again, the only way I can get it to work is if I throw a byte = new byte[4096] in the loop and it destroys memory... I know it seems very weird... i'm using 1.4
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
When you call InputStream.read(byte[]) it will not overrite the entire array, but will read what it can, put that in the first few elements of the array, leave the rest of the array alone and return the number of bytes it read. Sometimes it might fill up the entire array, but sometimes it might only read a few (or even zero) bytes.
So the only valid stuff in the byte array are the elements 0 thru bytesRead-1. The rest will be junk (leftovers from previous loops).
And this is what OutputStream.write(array,0,bytesRead) is supposed to pay attention to - only those first bytesRead bytes.
So if you have put println's in the loop make sure you also pay attention to the bytesRead value, so you are not printing junk that write() is ignoring.
Otherwise, what are you using for input and output streams - did you implement your own Filter Stream, for example - the problem could be in there?
[ October 11, 2002: Message edited by: Dave Landers ]
Shaun Brierly
Greenhorn

Joined: Aug 22, 2001
Posts: 17
The Print out is actually on the server... this is what it looks like...
[B@7b6889
[B@7b6889
[B@7b6889
[B@7b6889
[B@7b6889
[B@7b6889... etc
So this is what's coming out of the socket... I know my problem is with Java arrays, considering if I put array = new bytes[4196]; in the while loop the data is sent correctly, however the client's memory is brought to it's knees.
Shaun Brierly
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
That print says you have an array "[" of bytes "B" with an identityHashCode (memory location) "@" of 0x7b6889. Doesn't really tell you anything except that the loop is reusing the same array (which is exactly what I'd expect).
What really is the problem you are seeing? Is the data corrupt? I expect the problem may be somewhere other than the array itself (if the code you posted is really what you are doing).
Shaun Brierly
Greenhorn

Joined: Aug 22, 2001
Posts: 17
Hmmm, whoops, for some reason I thought the print out was scrambled data =( due to once I had transfered a text file, and it was repeated, telling me every chunk was the same, it appears to work, hehe thanks =)
Shaun Brierly
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Arrays do not have a useful toString() function, and unfortunately you can't subclass an array to provide your own overridden function. You'll need to write a loop if you want to print out array contents.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You could use
System.out.write(array, 0, bytesRead);
System.out.println();
but beware that if the bytes represent anything other than text data, you will get a lot of "funny" (unprintable) characters. In this case, write a loop as suggested.
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
You need not iterate through an array to print it (unless you're using primitives)
see java.util.Arrays.asList
To copy an array you can use System.arraycopy.
[ October 15, 2002: Message edited by: Garrett Smith ]

comp.lang.javascript FAQ: http://jibbering.com/faq/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Reference Problem....