Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

how to append a byte to an array of bytes

  Asked By: Adelina    Date: May 13    Category: Java    Views: 6320
  

I'd like to ask if anyone knows how to append a byte to an array of bytes..


I tried the following:

1:

byte[] a;

// some operation to give array "a" a some values

byte b;

// some operations to give "b" a value

byte c[a.length + 1];

c = a;

c[a.length + 1] = b;


2:


byte[] a;

// some operation to give array "a" a some values

byte b;

// some operations to give "b" a value

byte c[];

c = a;

c[a.length + 1] = b;


none of them worked.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Benny Torres     Answered On: May 13

your statements will not work

1: c = a;
2: c[a.length + 1] = b;

i guess in 2: you must be getting ArrayIndexOutOfBoundsException


following will work


byte [] c= new byte[a.length+1];
for(int i=0;i<a.length;i++) c[i]=a[i];
c[a.length]=b;

 
Answer #2    Answered By: Ulfah Hashmi     Answered On: May 13

Unless you have to use the array  notation, you may want to look at the
Vector or ArrayList classes. They automatically handle resizing, but
you have to use the wrapper classes and casts (at least until Java 1.5
is released).

 
Didn't find what you were looking for? Find more on how to append a byte to an array of bytes Or get search suggestion and latest updates.




Tagged: