Vector to String[] Conversion

Hi All,

I am trying to convert Vector to String Array. the problem is when I tried to print the String Array out side the loop I am getting only last value.

I want to pass String Array to another function.

How to do that?

Find the code below:-

Vector vec=new Vector()

vec.add("abc\\aa.doc");

vec.add("abc\\ccc.doc");

vec.add("abc\\bb.doc");

String [] arr=new String[vec.size()];

for(int i=0;i<=vec.size();i++){

arr[i]=vec.get(i).toString();

}

System.out.println("ARRAY"+ arr);

Current Output

Output :- ARRAY abc\\bb.doc

Desired Output

ARRAY abc\\aa.doc,,abc\ccc.doc,abc\\bb.doc

Thanks.

[991 byte] By [JavaNewUsera] at [2007-11-27 4:21:30]
# 1

List < String > vec=new Vector< String > ();

vec.add("abc\\aa.doc");

vec.add("abc\\ccc.doc");

vec.add("abc\\bb.doc");

String[] arr = vec.toArray(new String[0]);

Hippolytea at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 2

Or, if you are using an old version of Java:

List vec=new ArrayList ();

vec.add("abc\\aa.doc");

vec.add("abc\\ccc.doc");

vec.add("abc\\bb.doc");

String[ ] arr = (String[ ]) vec.toArray(new String[0]);

Hippolytea at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for the early reply

But,

If you see the code again, you will find that I am trying to print the values of ARRAY outside the for loop

for(int i=0;i<=vec.size();i++){

arr[i]=vec.get(i).toString();

}

System.out.println( "ARRAY"+ arr);

Even with your code I am getting the same result.

Current Output

Output :- ARRAY abc\\bb.doc

I want full vector converted to String Array out side the loop.

Desired Output

ARRAY abc\\aa.doc,,abc\ccc.doc,abc\\bb.doc

Thanks

JavaNewUsera at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 4
Why print out the values in the array, when you can print out the values in a collection so much more easily:System.out.println( "collection: "+ vec);
Hippolytea at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 5
The point is I have to pass String[] to a function, but all the values are there in Vector. So I need to convert vector into String[].Thanks.
JavaNewUsera at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 6
Object[] o=vector.toArray();String[] s=new String[o.length];System.arraycopy(o,0,s,0,o.length);myFunctionThatTakesAStringArray(s);
xiarcela at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 7
my previous code assumes that the Vector contains String objects
xiarcela at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 8

> Object[] o=vector.toArray();

> String[] s=new String[o.length];

> System.arraycopy(o,0,s,0,o.length);

>

> myFunctionThatTakesAStringArray(s);

Replies #1 (or #2) are better solutions because you don't go through the

silliness of copying from one array to another.

Hippolytea at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 9

Can you pass a List to this function?

public static void someSillyFunction(String[] args)

{

}

I thought I read that the OP needs to pass a the result to a method that takes String[].

If that is not the case, you are most likely right...

xiarcela at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 10

You should always post the real code you're using.

The code as you posted would throw an exception since you're trying to access the vector outside it's boundaries.

Your code:

for(int i=0;i<=vec.size();i++){

arr[i]=vec.get(i).toString();

}

Correct code:

for(int i = 0;i < vec.size(); i++) {

arr[i] = vec.get(i).toString(); // or cast to String

-Kayaman-a at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 11
Object[] o=vector.toArray();String[] s=new String[o.length];System.arraycopy(o,0,s,0,o.length);myFunctionThatTakesAStringArray(s);Hi ,I am getting an errorjava.lang.ArrayStoreException when I use the above code..Any other solution
JavaNewUsera at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 12
This assumes vec contains only Strings.String[ ] arr = (String[ ]) vec.toArray(new String[0]);Second verse, same as the first.
Hippolytea at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 13

OK,

My vector contains Objects...

I tried to use the following code

String[] attachments = new String[setOfHandle.size()];

for (int i = 0; i < setOfHandle.size(); i++) {

attachments[i] = String.valueOf(setOfHandle.get(i));

// if attachments exists, add them

if ( attachments != null ) {

addAtta( maild, attachments );

checkPoint = false;

}

}

In the above code setOfHandle is a vector contains objects.. I need to convert it to String[] and pass it to addAtta.

While doing this I am getting an error

java.lang.ArrayIndexOutOfBoundsException: 1

JavaNewUsera at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 14
What version of Java are you using?
Hippolytea at 2007-7-12 9:28:38 > top of Java-index,Java Essentials,Java Programming...
# 15
j2sdk1.4.2_14
JavaNewUsera at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 16
j2sdk1.4.2_14
JavaNewUsera at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 17

I'm not totall sure I understand your problem, but if it's what I think it is:

1) Determine the size of the Vector.

2) Create a new String[] of that size.

3) Iterate over the Vector

3.1) For each element in the Vector, call its toString and put the results into the current array slot. If you want null to go in as null, check first before calling toString. If you want it to go in as "null", don't bother checking, and call String.valueOf instead of toString.

jverda at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 18

> While doing this I am getting an error

> java.lang.ArrayIndexOutOfBoundsException: 1

You're trying to access the second element (index 1) of an array that has a length less than 2. Look at the details of the stack trace. It's already telling you what you did wrong. If you look more closely, you'll see exactly where it went wrong and what called what to get there.

jverda at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 19

String[] attachments = new String[setOfHandle.size()];

for (int i = 0; i < setOfHandle.size(); i++) {

attachments[i] = String.valueOf(setOfHandle.get(i));

}

It's a mystery to me what you are doing testing "attachments != null" in that loop. I suggest you stop that.

Hippolytea at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 20

> > String[] attachments = new

> String[setOfHandle.size()];

> for (int i = 0; i < setOfHandle.size(); i++) {

> attachments[i] =

> String.valueOf(setOfHandle.get(i));

>

>

> It's a mystery to me what you are doing testing

> "attachments != null" in that loop. I suggest you

> stop that.

He'd need to test if he wanted null instead of "null" if any of the Vector's elements could be null.

jverda at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 21
oops mistakeIf condition is outside the for loop.Hippolyte :- Still I am getting the same errorjverd - you are right I am exactly looking for that.
JavaNewUsera at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 22

My code works when I run it:

import java.awt.Point; //for example

import java.util.*;

public class ToArrayExample {

public static void main(String[] args) {

List list = new ArrayList();

list.add(new Point(1,2));

list.add(null);

list.add(new Point(3,4));

String[] arr = new String[list.size()];

for (int i = 0; i < list.size(); i++) {

arr[i] = String.valueOf(list.get(i));

}

//print out

for(int i = 0; i < arr.length; ++i) {

System.out.println(arr[i]);

}

}

}

Hippolytea at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 23
I know it works fine,But if you have to pass your String[] (In you example arr) to a function how you do that?
JavaNewUsera at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 24

In the usual way:

import java.awt.Point; //for example

import java.util.*;

public class ToArrayExample {

public static void main(String[] args) {

List list = new ArrayList();

list.add(new Point(1,2));

list.add(null);

list.add(new Point(3,4));

String[] arr = new String[list.size()];

for (int i = 0; i < list.size(); i++) {

arr[i] = String.valueOf(list.get(i));

}

display(arr);

}

public static void display(String[] s) {

for(int i = 0; i < s.length; ++i) {

System.out.println(s[i]);

}

}

}

Hippolytea at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 25
Thanks Hippolyte , I am doing exactly what you have shown ..but still I am getting the same error .. Now I predict probably its a problem with external API what I am using.I was cross checking my self ....Anyway Thanks.
JavaNewUsera at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 26
> In the usual way:for (int i = 0; i < list.size(); i++) {arr[i] = String.valueOf(list.get(i));}And this executes faster than arraycopy?
xiarcela at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...
# 27
> And this executes faster than arraycopy?Maybe.
Hippolytea at 2007-7-21 21:05:24 > top of Java-index,Java Essentials,Java Programming...