• Post Reply Bookmark Topic Watch Topic
  • New Topic

insert 4 bytes into an array  RSS feed

 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I easily insert 4 bytes into an array a specific starting address. or do I have to right code for it?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about directly modifying the computer's RAM at a specific address, or about something else? Java's not going to let you do the former without using some non-Java (native) code.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im only wanting to do it in a Java program with java arrays
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK: what kind of array is it? A byte[] or something else?

If it's a byte[], and the address (or "index") is "x" (which might be, say, 6) then you can just assign the bytes one at a time or in a loop:

byte[] myByteArray = new byte[100];
myByteArray[x] = myFirstByte;
myByteArray[x+1] = mySecondByte;
myByteArray[x+2] = myThirdByte;
myByteArray[x+3] = myFourthByte;

If it's not a byte[], then you'd have to describe how you want the bytes converted to the type of the array, whatever that is.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, the word you want is "index", not "address". The numbered position of a particular array element is its index, where the first item has index 0. People sometimes say "subscript" to mean the same thing as "index", but "address" has a lower-level meaning and is something that Java programs generally don't deal with.

Java arrays cannot be resized, so you cannot insert elements into them, in the true sense. However, if your array has been created larger than the current number of valid elements in it, then you can do something like insertion by shuffling the elements above the insertion point up by 4; you might use System.arraycopy() for that. Then you can put your 4 new items into the right places.

However, unless you really have to use arrays, you should instead use a Collection. These can be resized; in fact, they resize themselves automatically. If you use a List (e.g. java.util.ArrayList), you can insert items using the add(index, element) method.

If you are not using Java 5 or later, you have the added complication that Collections always contain Objects, whereas you want to store bytes. However, you can convert the raw bytes into Byte objects.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very helpful guys thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!