Making an object that can be various types?

I have a byte[] and a short[] used for the same thing. I want to switch from using the byte[] and short[] based on the maximum value needed, which changes occasionally. My idea is to have an Object that is swapped between referencing the byte[] or the short[] as needed. The part I can't figure out is casting the Object to the array type its referencing. I tried this to alter the contents of the array the object is referencing:
((array.getClass())(array))[pos] = ...
((array.getClass())(array))[pos] = ...
but it just says "the result of getClass() is unused" (aka it isnt trying to cast the Object with the result like i want it to)
13 Replies
JavaBot
JavaBotā€¢2w ago
āŒ› This post has been reserved for your question.
Hey @š“pothicon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1stā€¢2w ago
Why aren't you always using a short[]? but no you can't cast like that what you could do in principle is create wrapper classes for each type of array with a common (sealed) interface but I don't see much of a point in that tbh
š“pothicon
š“pothiconOPā€¢2w ago
memory usage this is for chunks of data with palette compression so if the palette only has 1 or 2 values i can use an array of booleans to reference that palette if it has 2-127 values i can use an array of bytes then shorts for values above that
ayylmao123xdd
ayylmao123xddā€¢2w ago
but it just says "the result of getClass() is unused" (aka it isnt trying to cast the Object with the result like i want it to) is this an error or a warning
š“pothicon
š“pothiconOPā€¢2w ago
warning
ayylmao123xdd
ayylmao123xddā€¢2w ago
o
š“pothicon
š“pothiconOPā€¢2w ago
it also says this
No description
š“pothicon
š“pothiconOPā€¢2w ago
if i hover over the red part
ayylmao123xdd
ayylmao123xddā€¢2w ago
you could ig use an if statement to know when to swap
š“pothicon
š“pothiconOPā€¢2w ago
yeah i can i just wanted something cleaner and less annoying to make plus it would be faster to not have to use those if statements
ayylmao123xdd
ayylmao123xddā€¢2w ago
one if statement doesnt sound too bad tbh so yea try that
š“pothicon
š“pothiconOPā€¢2w ago
yeah that reduced memory usage from 1.54gb to 1.03gb worth and the savings from adding the boolean array might be even bigger
JavaBot
JavaBotā€¢2w ago
Post Closed
This post has been closed by <@465515365108154380>.
Want results from more Discord servers?
Add your server