I have several fields, each of which looks as follows:
field1 field2 field3 ...
Using a loop with a counter, I want to say fieldx. Where x is the counter value in this loop. This means that if I have 6 entries in my array, fields1 to field6 will be given values.
Is field x possible?
You can do this using reflection , but in general it is better if you can declare your fields in an array. Instead:
SomeType field1; SomeType field2; SomeType field3; ... SomeType field6;
You can do it:
SomeType[] fields = new SomeType[6];
Then you can iterate over the array by setting the values:
for (int i = 0; i < fields.length; ++i) { fields[i] = yourValues[i]; }
, ol 'array (. "" ), Arraylist. :
Arraylist
ArrayList<SomeType> fields = new ArrayList<SomeType>();
( , fields.add(SomeType t), :
fields.add(SomeType t)
for (Sometype t : fields) { // Do stuff with t }
ArrayLists , .
ArrayLists
, Java 5 ! , , , length size(), for-each.
length
size()
, . java.lang.reflect, Field.
java.lang.reflect
Field
Source: https://habr.com/ru/post/1751642/More articles:How to embed iframe in facebook? - facebookFirefox css table generates kill table width - htmlAllow installation of only a specific class / instance - c #Is there anyone out there using Clear Case with Sybase Powerbuilder? - version-controlпытаясь увидеть, не имеет ли вектор значений в карте <строка, строкa> - c++progress bar on image upload - iphoneHow to get a large number as input and store it in memory - inputA TypeLoadException occurred while dragging a user control in VS Designer - c #How to kill php session with one button - htmlProblems printing IE8 using jqplot - jqueryAll Articles