populating array list problem

Hi all ,

Hope you can help me with this.

I have two arrays

ArrayList names = new ArrayList();

ArrayList values = new ArrayList();

The values arraylist is the size of names array

values = new ArrayList(names.size());

how do i iterate through the names array , get index of names array and add values to names according to index (of the names arrayList)?

Thanks for your helo

[430 byte] By [waj310a] at [2007-11-26 18:39:10]
# 1

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

values.add(new Integer(i));

}

This example will simply set the index as the value, you probably want to fetch the value from somewhere depending on the name.

Alternatively, have you looked into the Map yet? Using it you can set the name as a key and then bind a value to it, like this:

Map<String, String> map = new HashMap<String,String>();

map.put("henry", "rollins");

map.put("david", "bowie");

String lastname = map.get("david");

lastname will contain "bowie". You can store any object type in the map in both the key and the value, in the above example I limited that to two strings using generics.

gimbal2a at 2007-7-9 6:13:13 > top of Java-index,Java Essentials,New To Java...