Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vector not working 1

Status
Not open for further replies.

xtreemnet

Programmer
Aug 9, 2003
88
NZ
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(&quot;array : &quot;+ 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(&quot;Vector &quot; + k + &quot;: &quot;+ data);
}
}
}

output:

array : one
array : two
array : three

Vector 0: three
Vector 1: three
Vector 2: three

please help,

Thanks

 
Can you post your code again and put it between [ code ] and [ /code ] ?
 
Code:
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(&quot;one&quot;);
  v1.addElement(&quot;two&quot;);
  v1.addElement(&quot;three&quot;);

//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(&quot;array : &quot;+ 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(&quot;Vector &quot; + k + &quot;: &quot;+ data);
  }
 }
}
 
The problem is that you are adding 3 times the same &quot;temp[]&quot; byte array (reference) to your Vector.
If you create a new &quot;temp&quot; variable IN the loop, your problem is solved.
So move the following line INSIDE the for loop
Code:
     byte[] temp = new byte[6];
=====
So
Code:
  //Loop for reading each vector element f
  for (j = 0; j < 3; j++) {
     byte[] temp = new byte[6];
     //add j to 1st byte of the new array
     temp[0] = (byte) j;
 
PS : I also had to change another line because I got a compilation error :
Code:
     //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;   // extreemnet
       temp[i+1] = arr[i];  // holo
     }
 
your suggestion has worked. The error you got because of my mistake in pasting the code.

Thanks a lot,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top