EASY array question

alright, basically i'm trying to store both strings and ints in the same array, possible or impossible? thanks
[118 byte] By [bling604a] at [2007-9-27 7:42:12]
# 1
Hi,I think that you can't do this, but you can use a Vector. I think that you can also use List but I'm not sure(I don't use it) You should check in the doc.Hope it helps.S閎astien
souptiera at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 2
You can do it in an array of Objects and using wrappers for ints (Integer).Denis
Deudeua at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 3

well it is possible in Visual Basic... so i think that it is possible in Java.

MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")

MyDay = MyWeek(2) --' MyDay contains "Tue".

MyDay = MyWeek(4)' MyDay contains "Thu".

This is how it works in VB

It shouldn't be too different in Java.

kozak98a at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 4
Should be OK. Create an array of Objects. Strings can be put in directly, int via th Integer wrapper:Object[] array=new Object[10];array[0]="HELLO";array[1]=new Integer(12);
armalcolma at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 5
What is the difference between an array of Objects and a Vector? Is one better than the other? Does Vector use more memory?Thanks to answer.S閎astien
souptiera at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 6

The major difference is a Vector can be extensible.

(In fact it is an array with a fixed size, but when we want to add something and the size is too small,

a new larger array is created and the first array is copied in and the new data is added).

Vector doesn't use really more memory because it is an array. The only moment the Vector use more memory is when it grows. The old array will be cleared by the gc when needed.

The choice is made only if you want a fixed size or not or if the size is known when you

want to create the array.

About access time, the performances are good with sequential or random access in both cases.

Denis

Deudeua at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 7
Thank you Denis.Are you French?Dans ce cas : merci beaucoup.S閎astien
souptiera at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 8
for some reason when i tried the object array it didn't work, but i'll try it again. by the way, this will then work for 2D arrays too right? alright, thanks everyone.
bling604a at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 9
alright, that worked perfectly! one last question, when i wrap the int so it can be an object, to use it as an int again, do i have to do:Integer.parseInt(ArrayName[x][y].toString());or is there a shortcut?Thanks so much everyone
bling604a at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...
# 10
No, you just cast it back from Object to Integer and extract the int from the Integer by using its intValue() method, like this:int valxy = ((Integer)ArrayName[x][y]).intValue();
DrClapa at 2007-7-8 11:52:48 > top of Java-index,Archived Forums,Java Programming...