-
vector not working
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
-
Could you please have another go at explaining what it is you're trying to do with the data 'before' and 'after'?(Also, why on earth would you want to do this?)
ArchAngel.
O:-)
-
I have an exercise on Java to do. This is about implementing the sliding window error control on a client - Server application using UDP protocol. I have a command text file like :
Insert, StudId, FirstName, LastName, AssignmentFileName
Modify, StudId, FirstName,
Retrieve, StudId
and so on.
I need to send a byte array with the Datagram packet to the server. The server receives this and processes and returns the result to client.
Hence I need to break each command line into packets of byte array (of predefined size) for implementing the sliding window.
So first I break up the command string and store them in vector.
Then convert them into byte array to add sequence number for each packet and store the CRC for each packet within itself.
Then this packet is sent to server.
hope this makes sense,
-
Why not add the sequence number to the String first, *then* generate the byte array?
ArchAngel.
O:-)
-
In the sample case the string is small, but in reality this will be a long string which I need to break it up to make packets and number them. For numbering I need to add a sequence number byte for each byte array made from broken up string.
-
Here's the basic building block I've built - see if this helps:
Code:
public class Test {
public static void main(String[] args) {
byte sequenceNumber = 4;
String s = "Hello";
byte[] basicPacket = s.getBytes(); // Probably should specify character encoding...
byte[] completePacket = addSequenceNumber(basicPacket, sequenceNumber);
displayPacket("Before", basicPacket);
displayPacket("After ", completePacket);
}
public static byte[] addSequenceNumber(byte[] inPacket, byte sequenceNumber) {
// Make another byte array, one element larger to accomodate sequence number.
byte[] outPacket = new byte[inPacket.length + 1];
// Place the sequence number in the first element
outPacket[0] = sequenceNumber;
// Copy the contents of 'inPacket' into 'outPacket' after the sequence number.
System.arraycopy(inPacket, 0, outPacket, 1, inPacket.length);
// Return the completed packet!
return outPacket;
}
public static void displayPacket(String name, byte[] packet) {
System.out.print("Packet '" + name + "':\t");
for (int i = 0; i < packet.length; i++) {
System.out.print(packet[i] + "\t");
}
System.out.println();
}
}
will output:
Code:
C:\david\docs\forums\push_byte_array>java Test
Packet 'Before': 72 101 108 108 111
Packet 'After ': 4 72 101 108 108 111
ArchAngel.
O:-)
-
That's great. It works now.
Thanks a lot.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|