Hi
I am trying to make a vector of String objects and then convert these strings to byte array with an additional byte in the front. Then add this byte array to another vector. But after adding this byte array when I try to display, it displays the last element added for all the elements. Cannot see what's wrong. The code and output is as below:
import java.lang.System;
import java.util.Vector;
import java.util.Enumeration;
public class VectorApp {
public static void main(String args[]){
byte[] arr = new byte[5];
byte[] temp = new byte[6];
byte[] newarray = new byte[6];
Vector v1 = new Vector();
Vector v2 = new Vector();
String str = null;
String data = null;
int i = 0;
int j = 0;
int k = 0;
//Creating a vector of string object
v1.addElement("one"
v1.addElement("two"
v1.addElement("three"
//Creating a vector of byte array using the above vector
//and adding an integer to 1st byte
//Loop for reading each vector element f
for (j = 0; j < 3; j++) {
//add j to 1st byte of the new array
temp[0] = (byte) j;
//get the jth string from 1st vector
str = (String) v1.get(j);
//convert this string to a byte array
arr = str.getBytes();
//add the byte array made from the string of vector to this new array
//from position 1
for (i = 0; i <arr.length ; i++) {
temp[i+1] = arr;
}
//display this array
data = new String(temp);
System.out.println("array : "+ data);
//Add this new byte array to a new vector
v2.addElement(temp);
}
//display the elements from this new vector of array objects
for (k =0; k < 3; k++) {
newarray = (byte[]) v2.get(k);
data = new String(newarray);
System.out.println("Vector " + k + ": "+ data);
}
}
}
output:
array : one
array : two
array : three
Vector 0: three
Vector 1: three
Vector 2: three
please help,
Thanks
I am trying to make a vector of String objects and then convert these strings to byte array with an additional byte in the front. Then add this byte array to another vector. But after adding this byte array when I try to display, it displays the last element added for all the elements. Cannot see what's wrong. The code and output is as below:
import java.lang.System;
import java.util.Vector;
import java.util.Enumeration;
public class VectorApp {
public static void main(String args[]){
byte[] arr = new byte[5];
byte[] temp = new byte[6];
byte[] newarray = new byte[6];
Vector v1 = new Vector();
Vector v2 = new Vector();
String str = null;
String data = null;
int i = 0;
int j = 0;
int k = 0;
//Creating a vector of string object
v1.addElement("one"
v1.addElement("two"
v1.addElement("three"
//Creating a vector of byte array using the above vector
//and adding an integer to 1st byte
//Loop for reading each vector element f
for (j = 0; j < 3; j++) {
//add j to 1st byte of the new array
temp[0] = (byte) j;
//get the jth string from 1st vector
str = (String) v1.get(j);
//convert this string to a byte array
arr = str.getBytes();
//add the byte array made from the string of vector to this new array
//from position 1
for (i = 0; i <arr.length ; i++) {
temp[i+1] = arr;
}
//display this array
data = new String(temp);
System.out.println("array : "+ data);
//Add this new byte array to a new vector
v2.addElement(temp);
}
//display the elements from this new vector of array objects
for (k =0; k < 3; k++) {
newarray = (byte[]) v2.get(k);
data = new String(newarray);
System.out.println("Vector " + k + ": "+ data);
}
}
}
output:
array : one
array : two
array : three
Vector 0: three
Vector 1: three
Vector 2: three
please help,
Thanks