aspose file tools*
The moose likes Java in General and the fly likes Can I pass an array of ints to a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can I pass an array of ints to a class?" Watch "Can I pass an array of ints to a class?" New topic
Author

Can I pass an array of ints to a class?

Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
I created a class that excepts the following:
public cache (int pos, int vbit, int loctag, int[] locdata)
In my main program when I update the array of cache and assign a new value to the locdata array I always get back the last entries information when I view all of the cache elements. Can you pass an array of ints into a class, and maintain the data? Or do I need to use another method? Thank you in advance for any help.


We make a living by what we get, we make a life by what we give!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Sam,

Can you pass an array of ints into a class, and maintain the data?

I'm not exactly sure what you are asking here. You can certainly pass an array of anything into a method. I suppose what I don't understand is the part about maintaining the data. For example:

Every time you call the setCache() method, you will lose whatever was in this.cache prior to the call. Remember that an array is an Object. Now you could change the signature of the secCache() method to return the old cache like this:

Or you could have a method to append elements to the cache:

Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
Michael, thank you for the help. I do need to return the old cache, and your second function is what I need. Although that doesn't seem to work either. What I have is a main program that keeps and array the construtor cache with the 4 elements, and the 4 is the array of ints. I want to access the 4 element and bring back the correct values in that array. Can you explain to me why you don't just take in the old cache and then send it back?

Shouldn't it be:

BTW, your right politicians are like diapers!
[ March 02, 2003: Message edited by: Sam Moran ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Sam,
I guess I was just confused at what you wanted to do. I thought that you wanted to replace the old cache with a new one and return the old one. If all you need to do is get the cache and update the values then you just need a getter:

When you call getCache(), that returns a reference to cache. You can then update the values directly.
Michael Morris
Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
Michael & anyone else who happens to see this post, I am pulling my hair out!!! I have pared my program down to a simple test and I can not get it to return the correct results?!?!? I initialize an array data and then create 8 array with a class. See below:

What the hell am I doing wrong? Thank you all for any help, and thank you again Michael.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Sam,
Man you got burnt by Object Identity. Everytime you call chkcache[i] = new cache(data);
then every instance of cache has a refernce to the same array and everytime you change a value of data[] then it (maybe not so?) obviously changes the values in each instance of cache. All you have to do is move the declaration of data like this:



... I am pulling my hair out!!!

I understand that there are some excellant hair restoration products on the market now.
Michael Morris
Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
Michael, many thanks I do have an object identity problem, but I has have bad code!!! I do not completely understand, but I will look this up while I getting some chi-a-pet hair!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I pass an array of ints to a class?