0

An example:

 int x1 = 1;
 int x2 = 2;
 int x3 = 3;
 ...
 int xn = n;

As you can see the name and value of the variables are generated - in this case by a counter. Is this possible?How?

David Wong
  • 39
  • 1
  • 8
  • 2
    Yes. You can.. But array might be a better option. – TheLostMind Oct 09 '14 at 11:24
  • Yes . But for what cause as Java provides the easy option to wrap similar datatypes – Santhosh Oct 09 '14 at 11:26
  • 2
    Either you're very new in programming and you should learn about arrays or you need to explain why arrays wouldn't apply in your case. – Eric Darchis Oct 09 '14 at 11:48
  • in fact I just want to know if there are some syntax that can replace these lines in a short loop or whatever. So, it looks like there doesn't exist this kind of syntax in java? – David Wong Oct 21 '14 at 14:43

2 Answers2

3

I do not know how, but this is probably possible with some crazy Reflection hacking - do not try to do it, as it will produce code that is hard to maintain, hard to understand and vulnerable to hard-to-trace bugs.

Instead use Collections (do not use arrays if not absolutely needed), for example ArrayList.

LionC
  • 3,106
  • 1
  • 22
  • 31
  • Why shouldn't you use arrays? They're suitable for quite a lot, and they tend to be quicker than other data structures like linkedLists (at least in C++, I can't recall about Java) – Yann Oct 09 '14 at 12:16
  • @Yann4 Because their only advantage is very little performance in few cases. On the other side they lack interfaces, utility methods and all kinds of specific structure specifications and implementation options, also making them less descriptive. They just do not fit in the OOP-paradigm that the rest of the Java-library follows today. Performance on this level is not relevant in most cases, Arrays even scale worse depending on the use-case. – LionC Oct 09 '14 at 12:24
  • Maybe edit that into your answer, because without that, the swearing off of arrays is a little bizarre. And given that you can't tell the use case from the question, providing the reasoning is important. (Also, I'd like to see where you're getting your "very little performance increase" data from, as that seems odd to me) – Yann Oct 09 '14 at 12:27
  • I didnt put that into the answer because it is actually meant as a general statement. If you just need to iterate over a HUGE number of objects without having the time to develop or use a class that is specifically made for this, use arrays. Else, dont. As that is not the point of the question, adding this design-discussion statement seemed kinda odd to me. (And for the sake of other visitors, I think we should continue this discussion in a chat and not here ;-) – LionC Oct 09 '14 at 12:30
  • Well you listed all of the java collections as valid options, but called the 1st and most obvious go-to out as something that you shouldn't do, without any reasoning. I just thought that that deserved justifying. – Yann Oct 09 '14 at 12:32
  • @Yann4 Basically, some points are covered well [here](http://www.jasonwhaley.com/blog/2010/09/28/an-array-of-reasons-to-not-use-arrays-in-java/), if you are interested in discussing this further Id be happy to do so (just create a chat room or send me an email) – LionC Oct 09 '14 at 12:38
  • Yeah, I'm stopping commenting now, because this is a little long, but FWIW, the lack of the info (that you've put in the comments) in the answer is what has stopped me +1-ing – Yann Oct 09 '14 at 12:39
  • @Yann4 I edited a link to the answer for those that want to understand why - thanks for your feedback :-) – LionC Oct 09 '14 at 12:42
-2

Yes, it is valid Java code, but in this particular case you would just use the numbers :D

CalibeR.50
  • 370
  • 2
  • 13
  • 2
    I think you (or if not I :D) misunderstood the question - he wants to declare such variables with some kind of pattern (aka programatically) – LionC Oct 09 '14 at 11:29