Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alexey Saenko
Ranch Hand
Posts: 30
Chrome Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alexey Saenko
Ranch Hand
Posts: 30
Chrome Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic