How to convert an Integer Vector to int array

I need an int[] array for which I do not know the length at compilation time.

So I am using a Vector vector = new Vector(), the elements of which I define as of type Integer.

However, I have a method which calls an int[] array and not a Vector.

How can I convert my vector to an int[] array?

I cannot just use vector.toArray(), because this makes an Object[] array, out of which I can make an Integer[] array, but not an int[] array.

How can I make an int[] array?

Thanks for looking at this!

[536 byte] By [BrigitAnanyaa] at [2007-11-27 8:40:58]
# 1
I'm not sure if there is a built in method for this. So write your own. Get the size of the Vector and create an array of that size. Then iterate over the Vector, extract the int value and insert into the array.
floundera at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 2

Well, that's exactly what I don't want to do. I don't want to say:

int[] array = new int[vector.size()];

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

{

array = ((Integer)vector.elementAt(i)).intValue();

}

because wouldn't this cause a memory problem, since the vector.size() is not known at compilation?

However, if this works, I could as well use my int[] array from the very beginning, and not use the vector at all.

BrigitAnanyaa at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 3
Why are people so worried about memory problems? How many numbers are you planning on inserting into your array/Vector?P.S. When posting code, use code tags to retain formatting. Copy code from source, paste, highlight, click code button.
floundera at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 4
Well, my array contains the r, g, b values of an image, and the size could easily by 30 million.
BrigitAnanyaa at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 5

I do get java.lang.OutOfMemoryError if I use an int[] array with a fixed length, which is not known at compile time.

So I do have to defined the array as a Vector of Integers with variable length.

But I do need to know how to convert it to an int[] array, because I need this format when calling the array.

Please help! Thanks a lot!

BrigitAnanyaa at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 6

The only way to convert a Vector (with Integers in them) to an array of int-primitives, is to loop through the entire Vector. Period.

And as flounder already pointed out: don't worry about memory. Just make sure you have something that works as it's supposed to. After completion, you can improve performance, if that is an issue at all.

prometheuzza at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 7
> After completion, you can> improve performance, if that is an issue at all.Improve performance by increasing the computer RAM?
achyuthba at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 8
> ...> Improve performance by increasing the computer RAM?Did you forget the smiley, or what?
prometheuzza at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 9
The smile is there on my Face..:-) :-)
achyuthba at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 10
> The smile is there on my Face..> :-) :-)Ok. I thought you were being serious.
prometheuzza at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 11
Serious and me... Never.But i am jealous of you. For earning so much Duke Dollers.:-)
achyuthba at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 12
> ...> But i am jealous of you. For earning so much Duke> Dollers.> :-)They're worth nothing, you know that right? Besides, if you calculate our Duke ratio's (Duke Stars divided by post count), we're practically the same!
prometheuzza at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 13

Well, in C++ you can(!!!) convert a vector<int> v to a pointer int *vPtr by saying

vPtr = &v.front(),

so you can call it from a method that calls an int* pointer and not a vector<int>.

Vectors would be pretty useless if you can only call them from methods that call Vectors.

I don't believe that you can't convert them in Java! And, yes, I do get a java.lang.OutOfMemoryError if I use an array instead of a Vector.

How is the conversion done in Java?

BrigitAnanyaa at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 14

> Well, in C++ you can(!!!) convert a vector<int> v to

> a pointer int *vPtr by saying

> vPtr = &v.front(),

> so you can call it from a method that calls an int*

> pointer and not a vector<int>.

>

> Vectors would be pretty useless if you can only call

> them from methods that call Vectors.

I have a surprise for you: Java is not the same as C++.

> I don't believe that you can't convert them in Java!

It really, really, REALLY is NOT possible!!!

> And, yes, I do get a java.lang.OutOfMemoryError if I

> use an array instead of a Vector.

>

> ...

You are probably doing something else wrong. Impossible to say without seeing your code though.

prometheuzza at 2007-7-12 20:39:42 > top of Java-index,Java Essentials,Java Programming...
# 15
If you are working with pixel values for large images, then where did this Vector come from? Why are you using a vector?
BigDaddyLoveHandlesa at 2007-7-21 22:45:10 > top of Java-index,Java Essentials,Java Programming...
# 16
> Well, in C++ you can(!!!) convert a vector<int> v to a pointer int *vPtr by saying> vPtr = &v.front(),You're just reminding us how nasty C++ is.
BigDaddyLoveHandlesa at 2007-7-21 22:45:10 > top of Java-index,Java Essentials,Java Programming...
# 17

You shouldn't be using a Vector unless you have a good reason to do so: Vector is old and slow. Better to use an implementation of a List.

Let me emphasize: you cannot convert a Vector (or a List for that matter) into a "corresponding" array of primitives. PERIOD!!!

What you need is something (outisde of the standard classes from Sun) that will keep your values as primitives. Apache has such a project: it's called the Commons Primitives project:

http://jakarta.apache.org/commons/primitives/

try the IntCollection:

http://jakarta.apache.org/commons/primitives/api-release/org/apache/commons/collections/primitives/IntCollection.html

prometheuzza at 2007-7-21 22:45:10 > top of Java-index,Java Essentials,Java Programming...