For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > Adding integers to a Vector??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Adding integers to a Vector??
bunallo

2005-02-21, 9:00 pm

If I want to add 1,2,3,4 to a vector I can only make it work like this:

Vector v = new Vector();

v.add(1+"");
v.add(2+"");
v.add(3+"");
v.add(4+"");

But it does not look very smart, is there some better way?


Daniel Tahin

2005-02-21, 9:00 pm

Yes. Create first an Integer object Integer tmp = new Integer(1);
v.add(tmp);
The same way for the other numbers!




bunallo schrieb:
> If I want to add 1,2,3,4 to a vector I can only make it work like this:
>
> Vector v = new Vector();
>
> v.add(1+"");
> v.add(2+"");
> v.add(3+"");
> v.add(4+"");
>
> But it does not look very smart, is there some better way?
>
>

Tilman Bohn

2005-02-21, 9:00 pm

In message <cvd2s6$dhu$1@news.net.uni-c.dk>,
bunallo wrote on Mon, 21 Feb 2005 17:44:37 +0100:

> If I want to add 1,2,3,4 to a vector I can only make it work like this:
>
> Vector v = new Vector();
>
> v.add(1+"");
> v.add(2+"");
> v.add(3+"");
> v.add(4+"");
>
> But it does not look very smart, is there some better way?


v.add( new Integer( 1 ) );

Also, unless you're multi-threading, you should take a look at
ArrayList instead.

--
Cheers, Tilman

`Boy, life takes a long time to live...' -- Steven Wright
Grant Wagner

2005-02-21, 9:00 pm

"bunallo" <nmnm@alala.com> wrote in message
news:cvd2s6$dhu$1@news.net.uni-c.dk...
> If I want to add 1,2,3,4 to a vector I can only make it work like
> this:
>
> Vector v = new Vector();
>
> v.add(1+"");
> v.add(2+"");
> v.add(3+"");
> v.add(4+"");
>
> But it does not look very smart, is there some better way?


A Vector stores objects, by concatenating each int with a String, you
are storing Strings in the Vector, not ints. You should probably use an
Array. Initialize it to a reasonable size and grow/copy it if you
require more space. Of course, if all you really want to do is store 1,
2, 3 and 4, you could use:

int[] v = { 1, 2, 3, 4 };

If you still feel you need to use a Vector, you probably want to store
Integer objects, not Strings:

Vector v = new Vector();
for (int ii = 1; ii < 5; ++ii)
{
v.add(new Integer(ii));
}

to retrieve the values:

for (int ii = 0; ii < v.size(); ++ii)
{
System.out.println((Integer) (v.get(ii)).intValue());
}

But if you still think you need to store the int values 1, 2, 3, 4, ...,
n in a Vector, I'd re-examine your design.

--
Grant Wagner <gwagner@agricoreunited.com>


Joerg Simon

2005-02-21, 9:00 pm

bunallo wrote:
> If I want to add 1,2,3,4 to a vector I can only make it work like this:
>
> Vector v = new Vector();
>
> v.add(1+"");
> v.add(2+"");
> v.add(3+"");
> v.add(4+"");
>
> But it does not look very smart, is there some better way?
>
>


Hm, if you want/can use java5 you may use generics...

Vector<int> v = new Vector<int>();

v.add(1);
v.add(2);

And it has the good side that the compiler can check type safty for your
Vector

--
--------------------------------------------------------------
----
||oerg (das ist ein j!)
\|| Simon ICQ: 49154322
\| email: j_simon@sbox.TUGraz.at
Grant Wagner

2005-02-22, 9:00 pm

"Joerg Simon" <j_simon@sbox.tugraz.at> wrote in message
news:421a2d06$0$12126$3b214f66@aconews.univie.ac.at...
> bunallo wrote:
>
> Hm, if you want/can use java5 you may use generics...
>
> Vector<int> v = new Vector<int>();
>
> v.add(1);
> v.add(2);
>
> And it has the good side that the compiler can check type safty for
> your Vector


Test.java:##: unexpected type
found : int
required: reference
Vector<int> v = new Vector<int>();
^
Test.java:##: unexpected type
found : int
required: reference
Vector<int> v = new Vector<int>();
^
2 errors

You can't put an int into a Vector. The following does work:

Vector<Integer> v = new Vector<Integer>();
v.add(new Integer(1));
v.add(new Integer(2));

but it just repeats what other responders have already said, and makes
the code dependant on generics.

--
Grant Wagner <gwagner@agricoreunited.com>


Joerg Simon

2005-02-24, 3:59 am

Grant Wagner wrote:
[snip]
> Test.java:##: unexpected type
> found : int
> required: reference
> Vector<int> v = new Vector<int>();
> ^
> Test.java:##: unexpected type
> found : int
> required: reference
> Vector<int> v = new Vector<int>();
> ^
> 2 errors


Hm, got it too, however, I was completely shure that I used exactly
something like the obove for an ArrayList in a GroupingSystem
Application... @_@ ...

> You can't put an int into a Vector. The following does work:
>
> Vector<Integer> v = new Vector<Integer>();
> v.add(new Integer(1));
> v.add(new Integer(2));
>
> but it just repeats what other responders have already said, and makes
> the code dependant on generics.
>


So, whats the poblem with make the code depandant on generics?
o You have compile time type safty
o The compiled code is still backward compatible....
o You don't have to cast like a maniac
o hm, you have _compile time_ type safty especially in your collections

However,
Exceuse me the mistake && my english

Yours,
Jörg Simon

--
--------------------------------------------------------------
----
||oerg (das ist ein j!)
\|| Simon ICQ: 49154322
\| email: j_simon@sbox.TUGraz.at
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com