Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Can I pass an array of ints to a class?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
reply
    Bookmark Topic Watch Topic
  • New Topic