I will start by assuming that you are forbidden to use even primitive types of Java arrays. In this case, you will have to go back to the basics and start managing the memory yourself. You will need to start by allocating a byte array or ByteBuffer of the appropriate size. Then access to this data block will be processed using what would be simple math pointer in C.
Example 1D array of 16 ints:
byte[] mem = new byte[16 * 4] ; // Ints are 4 bytes long // Write a value to the 8th element of our "array" index = 8 ; int value = 31415926 ; mem[4 * index + 0] = (byte)( value >> 24 ) ; mem[4 * index + 1] = (byte)( ( value << 8 ) >> 24 ) ; mem[4 * index + 2] = (byte)( ( value << 16 ) >> 24 ) ; mem[4 * index + 3] = (byte)( ( value << 24 ) >> 24 ) ;
Reading a value outside this would be reversing the process and calculating the stored int value.
Note. The process of setting and getting values ββis simpler with ByteBuffer, as it allows you to get and put primitive Java types into an array of bytes.
source share