aspose file tools*
The moose likes Beginning Java and the fly likes creating an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating an array" Watch "creating an array" New topic
Author

creating an array

renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
Hi,
I want the values to be stored in the countries array as below. Below is the javascript code written in JSP. Can anyone please tell me how can i set the values to an array using java by means of for loop. Thanks

var countries=new Array();
countries[0]=new Array();
countries[0]['country']='United Kingdom';
countries[0]['cities']=['London','Manchester','Birmingham','Liverpool','Edinburgh','Cardiff','Belfast'];
countries[1]=new Array();
countries[1]['country']='United States';
countries[1]['cities']=['Washington DC','New York','Los Angeles','Chicago'];
countries[2]=new Array();
countries[2]['country']='Australia';
countries[2]['cities']=['Canberra','Melbourne','Sydney','Brisbane'];


Cheers,
Richard
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

It looks like you want an expandable array. Unlike JavaScript arrays, Java arrays have a fixed size. You'll want to use a List instead.

However, a List has only an integer-based index. For other types of indexes you will need a Map instead.

A little example (excluding generics for the moment):


You might want to encapsulate the country + city information into a new class though, just to make it easier:


Instead of a lot of casting and Map accessing, where you can mistype the name (e.g. "Country" instead of "country") you have one single object that has all information.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating an array
 
Similar Threads
function firing before page loads?
Values not poulating in the dropdown box.
Populating drop down from Database
Arrays in JSP
Populating dropdown menu based on another