This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to name and access an array which its name is not specified? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to name and access an array which its name is not specified?" Watch "How to name and access an array which its name is not specified?" New topic
Author

How to name and access an array which its name is not specified?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
I face with a complex case which makes me confused,it's about how to name and access an array which its name is not exactly specified!! let me explain.
I have to build several array which their name are not axactly specified and is depends on the time which "for" block will be executed. e.g.:


here, BookBuyerAgent+i is suppose to be my array name. Then, I will have number of arrays called as: BookBuyerAgent0, BookBuyerAgent1, BookBuyerAgent2,...

now I need to have access to those arrays in another block, but I dont know even how can I make it???: what I need is some thing like this:

but it doesnt work.

any idea please??

Thank you so much,
Sahar.
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

you can't use a runtime value for a variable name.

what i can suggest is create a map and put a String , array pair in it. you can generate the string value at runtime.

sample code can be


you can access the ArrayList using name key.
avi sinha


SCJP 5.0 SCWCD 5.0
Alexey Saenko
Ranch Hand

Joined: Aug 18, 2008
Posts: 30

Hi Sahar,

Of course, this snippet won't work, because any using of quoter in the code says to jvm create a new String object or get a String object from the pool. And, also, it is not possible to name variables and methods in runtime (for this you should read more about java reflections).

In java all two- dimensional arrays are not tables like in other programming languages. They are arrays of arrays. Therefore, you can make what you want by creating an array of arrays in the following way:


and then use it like:

and so on.


SCJP 6, SCBCD 5, OCEWSD 6
My blog: http://darkleden.wordpress.com
Alexey Saenko
Ranch Hand

Joined: Aug 18, 2008
Posts: 30

And just a little addition,

avi sinha wrote:

you can access the ArrayList using name key.


Yes, it is right, but don't forget to use generics in map and list declarations. Also, it is recommended to use only interfaces for types:
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

ya its correct and of course a better way to implement collection classes along with generics but in my opinion for a newbie at first they should have
some experience of using Collection classes(without generics).

and generics should be used, when they can understand why it is being used and when they can distinguish its effects.


avi sinha

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Woow! Dear avi sinha and Alexey Saenko Thank you so much for your very complete and usefull advise!!
I love this forum!!

Sahar.
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

sahar sa wrote:Woow! Dear avi sinha and Alexey Saenko Thank you so much for your very complete and usefull advise!!
I love this forum!!

Sahar.


we too.

avi sinha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to name and access an array which its name is not specified?
 
Similar Threads
Array declaration
Array
doubt in Array construction
Multi dimensional arrays
Sun Cirtification