dynamic double array

B

Bernd Pocher

I will save a lot of doubles in a array. At the start I don't know how many
doubles I get from the user. So I'm looking for a vector which used doubles.
Has java a vector which works with doubles? Or how can I resize 'double[]
value'?

Bernd
 
K

klynn47

Vectors only hold object references, but you could store the doubles in
Double and store them in the Vector and then later create an array of
doubles whose size is equal to the size of the Vector and then one by
one turned the elements in the Vector into doubles.

Another way would be to concatenate each new double into a String and
delimit the doubles with some character like ;, and then use split to
turn it into an array of Strings, and then create an array of double of
the same size and one by one turn the Strings into doubles.
 
S

Steve W. Jackson

Bernd Pocher said:
I will save a lot of doubles in a array. At the start I don't know how many
doubles I get from the user. So I'm looking for a vector which used doubles.
Has java a vector which works with doubles? Or how can I resize 'double[]
value'?

Bernd

You can't resize any array; instead, it would be necessary to create a
replacement and copy all your data over before disposing of the old one.
That could get costly.

But have you considered using the Double class? Any class can be put
into a Vector or ArrayList, and those of course automatically grow as
required. The Double class is easy to use except that it's immutable,
so that changing any value requires replacing the Double object with
another one having the new value.

= Steve =
 
R

Ryan Stewart

Bernd Pocher said:
I will save a lot of doubles in a array. At the start I don't know how many
doubles I get from the user. So I'm looking for a vector which used doubles.
Has java a vector which works with doubles? Or how can I resize 'double[]
value'?
Depending on what "a lot" is, you might want to create your own Collection-like
class to handle them. The class would begin with a certain size array which
would grow by a certain size when it is filled rather than growing every time an
element is added.
 
C

Chris Smith

Ryan Stewart said:
Bernd Pocher said:
I will save a lot of doubles in a array. At the start I don't know how many
doubles I get from the user. So I'm looking for a vector which used doubles.
Has java a vector which works with doubles? Or how can I resize 'double[]
value'?
Depending on what "a lot" is, you might want to create your own Collection-like
class to handle them. The class would begin with a certain size array which
would grow by a certain size when it is filled rather than growing every time an
element is added.

Again depending on what you mean by "a lot", you should also consider
expanding the size by some factor (for example, doubling the size) when
you need to increase it rather than adding a constant amount. There are
serious advantages in asymptotic complexity here, at the cost of a
potential increase in the memory needed for small instances of the
problem.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

Bernd Pocher

How could show a fast algorithm to expand an array with n new Elements at
Position index?

protected void Resize(int Index, int n = 1) {
try {
Capacity = (Capacity * 2) + 1; // Capacity = Lenght
of a Array, at start Capacity = 0
if (Capacity < (n + Count)) Capacity += n; // Count is the real needed
Capacity

double[] tmpval = new double[Capacity]; // Create new Array
//??? Value = tmpval // Value is the
double-Array of the class
//...
}
}

Bernd

Ryan Stewart said:
Bernd Pocher said:
I will save a lot of doubles in a array. At the start I don't know how many
doubles I get from the user. So I'm looking for a vector which used doubles.
Has java a vector which works with doubles? Or how can I resize 'double[]
value'?
Depending on what "a lot" is, you might want to create your own Collection-like
class to handle them. The class would begin with a certain size array which
would grow by a certain size when it is filled rather than growing every time an
element is added.
 
T

Tor Iver Wilhelmsen

Bernd Pocher said:
I will save a lot of doubles in a array. At the start I don't know how many
doubles I get from the user. So I'm looking for a vector which used doubles.
Has java a vector which works with doubles? Or how can I resize 'double[]
value'?

You can use 1.5's typed collections and autoboxing:

ArrayList<Double> doubles = new ArrayList<Double>();

doubles.add(3.14); // Automatically creates a Double

double value = doubles.get(0); // Converts from Double
 
C

Chris Uppal

Tor said:
You can use 1.5's typed collections and autoboxing:

If the OP's comment:

is taken seriously (i.e. hundreds of thousands or millions) then this is bad
advice in that it will consume massive amounts of completely unecessary space.

If the OP is going to want to do much maths on the resulting collection (array
multiplication and so on) then the overhead of autoboxing should not be
ignored. I'd expect it to dominate the runtime.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top