How to add variables to list if there is many variables?
I am learning JAVA. If I want to add some variables on list. What I know how is like this:
String a = "a";
String b = "b";
String c = "c";
List<String> verifyItem = Arrays.asList(a, b, c);
How about if I have many variables. For example, from a to z, do I need to manually add it like that?
List<String> verifyItem = Arrays.asList(a, b, c, d, ..., y, z);
Because I declared 100+ variables and need to add them on list. Do we have other approaches to add them smarter? Any hotkey in Eclipse or Intellij?
Thanks for your help.
java
|
show 5 more comments
I am learning JAVA. If I want to add some variables on list. What I know how is like this:
String a = "a";
String b = "b";
String c = "c";
List<String> verifyItem = Arrays.asList(a, b, c);
How about if I have many variables. For example, from a to z, do I need to manually add it like that?
List<String> verifyItem = Arrays.asList(a, b, c, d, ..., y, z);
Because I declared 100+ variables and need to add them on list. Do we have other approaches to add them smarter? Any hotkey in Eclipse or Intellij?
Thanks for your help.
java
1
I hardly think so. But wouldn't it be smarter to just create these variables in the list in the first place?
– Tu.ma
Nov 20 '18 at 9:15
4
Ideally you shouldn't declare 100 variables, you could've used a collection. Now to add all of them to the list, you can iterate over the original collection and add them to the list.
– Nicholas K
Nov 20 '18 at 9:16
Are the variables incremental? As in the example in you have given, is there a pattern to the variables, as you may be able to create them directly to the list in a loop
– Kleo G
Nov 20 '18 at 9:16
Have a look at this: stackoverflow.com/questions/15112590/…
– Robert Kock
Nov 20 '18 at 9:18
1
If you have 100 variables like that, it is probably not a good design. Can you tell us why you need 100+ variables?
– MC Emperor
Nov 20 '18 at 9:59
|
show 5 more comments
I am learning JAVA. If I want to add some variables on list. What I know how is like this:
String a = "a";
String b = "b";
String c = "c";
List<String> verifyItem = Arrays.asList(a, b, c);
How about if I have many variables. For example, from a to z, do I need to manually add it like that?
List<String> verifyItem = Arrays.asList(a, b, c, d, ..., y, z);
Because I declared 100+ variables and need to add them on list. Do we have other approaches to add them smarter? Any hotkey in Eclipse or Intellij?
Thanks for your help.
java
I am learning JAVA. If I want to add some variables on list. What I know how is like this:
String a = "a";
String b = "b";
String c = "c";
List<String> verifyItem = Arrays.asList(a, b, c);
How about if I have many variables. For example, from a to z, do I need to manually add it like that?
List<String> verifyItem = Arrays.asList(a, b, c, d, ..., y, z);
Because I declared 100+ variables and need to add them on list. Do we have other approaches to add them smarter? Any hotkey in Eclipse or Intellij?
Thanks for your help.
java
java
edited Nov 20 '18 at 9:40
MC Emperor
8,187125388
8,187125388
asked Nov 20 '18 at 9:13
sgsdgdsgsd sgsdgdsgsd
32
32
1
I hardly think so. But wouldn't it be smarter to just create these variables in the list in the first place?
– Tu.ma
Nov 20 '18 at 9:15
4
Ideally you shouldn't declare 100 variables, you could've used a collection. Now to add all of them to the list, you can iterate over the original collection and add them to the list.
– Nicholas K
Nov 20 '18 at 9:16
Are the variables incremental? As in the example in you have given, is there a pattern to the variables, as you may be able to create them directly to the list in a loop
– Kleo G
Nov 20 '18 at 9:16
Have a look at this: stackoverflow.com/questions/15112590/…
– Robert Kock
Nov 20 '18 at 9:18
1
If you have 100 variables like that, it is probably not a good design. Can you tell us why you need 100+ variables?
– MC Emperor
Nov 20 '18 at 9:59
|
show 5 more comments
1
I hardly think so. But wouldn't it be smarter to just create these variables in the list in the first place?
– Tu.ma
Nov 20 '18 at 9:15
4
Ideally you shouldn't declare 100 variables, you could've used a collection. Now to add all of them to the list, you can iterate over the original collection and add them to the list.
– Nicholas K
Nov 20 '18 at 9:16
Are the variables incremental? As in the example in you have given, is there a pattern to the variables, as you may be able to create them directly to the list in a loop
– Kleo G
Nov 20 '18 at 9:16
Have a look at this: stackoverflow.com/questions/15112590/…
– Robert Kock
Nov 20 '18 at 9:18
1
If you have 100 variables like that, it is probably not a good design. Can you tell us why you need 100+ variables?
– MC Emperor
Nov 20 '18 at 9:59
1
1
I hardly think so. But wouldn't it be smarter to just create these variables in the list in the first place?
– Tu.ma
Nov 20 '18 at 9:15
I hardly think so. But wouldn't it be smarter to just create these variables in the list in the first place?
– Tu.ma
Nov 20 '18 at 9:15
4
4
Ideally you shouldn't declare 100 variables, you could've used a collection. Now to add all of them to the list, you can iterate over the original collection and add them to the list.
– Nicholas K
Nov 20 '18 at 9:16
Ideally you shouldn't declare 100 variables, you could've used a collection. Now to add all of them to the list, you can iterate over the original collection and add them to the list.
– Nicholas K
Nov 20 '18 at 9:16
Are the variables incremental? As in the example in you have given, is there a pattern to the variables, as you may be able to create them directly to the list in a loop
– Kleo G
Nov 20 '18 at 9:16
Are the variables incremental? As in the example in you have given, is there a pattern to the variables, as you may be able to create them directly to the list in a loop
– Kleo G
Nov 20 '18 at 9:16
Have a look at this: stackoverflow.com/questions/15112590/…
– Robert Kock
Nov 20 '18 at 9:18
Have a look at this: stackoverflow.com/questions/15112590/…
– Robert Kock
Nov 20 '18 at 9:18
1
1
If you have 100 variables like that, it is probably not a good design. Can you tell us why you need 100+ variables?
– MC Emperor
Nov 20 '18 at 9:59
If you have 100 variables like that, it is probably not a good design. Can you tell us why you need 100+ variables?
– MC Emperor
Nov 20 '18 at 9:59
|
show 5 more comments
3 Answers
3
active
oldest
votes
Because I declared 100+variables and need to add them on list.Do we have other approach to add them smarter? Any hotkey in Eclipse or Intellij?
I don't think declaring 100+ variable inside the code is a good idea. In the future, another coder who read the code will be confused with this 100 variable.
If I were you, I will create a separate file to store the 100+ variables value. Then, I'll create a function to read them.
//Let say you have file called variable.txt
BufferedReader br = new BufferedReader(new FileReader("variable.txt"));
try {
String line = br.readLine();
List<String> verifyItem = new ArrayList<String>();
while (line != null) {
verifyItem.add(line);
line = br.readLine();
}
} finally {
br.close();
}
//do something with verifyItem
Note: you can also change List<String> verifyItem
into HashMap<String, String> verifyItem
if you would like to access it easily.
It's up to you. It depends on the way you use verifyItem
.
I hope this answer will help you.
add a comment |
If like to collect all the fields in same class you can do it by using java reflection, below is example
Fields class
class Fields{
String name= "hello";
int age =10;
}
Main class collect filed Names into List
and Name, values into Map
public static void main(String args) {
Fields fields = new Fields();
Field f= fields.getClass().getDeclaredFields();
List<String> fieldNames=Arrays.stream(f).map(i->i.getName()).collect(Collectors.toList());
Map<String, Object> fieldNameValues = Arrays.stream(f).
collect(Collectors.toMap(Field::getName, i->{
try {
return i.get(fields);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}));
System.out.println(fieldNames); //[name, age]
System.out.println(fieldNameValues); //{name=hello, age=10}
}
add a comment |
String values = new String {...};
ArrayList<String> list = new ArrayList<String>();
for (String temp : values) {
list.add(temp);
}
List is an interface that defines some common lists's things. I think ArrayList should suit your needs.
2
If you can add variables as an Array what is point of iterating array to create a list. You can use List<String> list = Arrays.asList(values); instead
– Gihan Saranga Siriwardhana
Nov 20 '18 at 9:36
That was an example of using ArrayList<>. Of course, if code only has variables instead of array, author can simplify this step.
– Dima Rich
Nov 20 '18 at 9:51
This code converts any array to an ArrayList and has nothing to do with the problem described in the question.
– f1sh
Nov 20 '18 at 9:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389666%2fhow-to-add-variables-to-list-if-there-is-many-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because I declared 100+variables and need to add them on list.Do we have other approach to add them smarter? Any hotkey in Eclipse or Intellij?
I don't think declaring 100+ variable inside the code is a good idea. In the future, another coder who read the code will be confused with this 100 variable.
If I were you, I will create a separate file to store the 100+ variables value. Then, I'll create a function to read them.
//Let say you have file called variable.txt
BufferedReader br = new BufferedReader(new FileReader("variable.txt"));
try {
String line = br.readLine();
List<String> verifyItem = new ArrayList<String>();
while (line != null) {
verifyItem.add(line);
line = br.readLine();
}
} finally {
br.close();
}
//do something with verifyItem
Note: you can also change List<String> verifyItem
into HashMap<String, String> verifyItem
if you would like to access it easily.
It's up to you. It depends on the way you use verifyItem
.
I hope this answer will help you.
add a comment |
Because I declared 100+variables and need to add them on list.Do we have other approach to add them smarter? Any hotkey in Eclipse or Intellij?
I don't think declaring 100+ variable inside the code is a good idea. In the future, another coder who read the code will be confused with this 100 variable.
If I were you, I will create a separate file to store the 100+ variables value. Then, I'll create a function to read them.
//Let say you have file called variable.txt
BufferedReader br = new BufferedReader(new FileReader("variable.txt"));
try {
String line = br.readLine();
List<String> verifyItem = new ArrayList<String>();
while (line != null) {
verifyItem.add(line);
line = br.readLine();
}
} finally {
br.close();
}
//do something with verifyItem
Note: you can also change List<String> verifyItem
into HashMap<String, String> verifyItem
if you would like to access it easily.
It's up to you. It depends on the way you use verifyItem
.
I hope this answer will help you.
add a comment |
Because I declared 100+variables and need to add them on list.Do we have other approach to add them smarter? Any hotkey in Eclipse or Intellij?
I don't think declaring 100+ variable inside the code is a good idea. In the future, another coder who read the code will be confused with this 100 variable.
If I were you, I will create a separate file to store the 100+ variables value. Then, I'll create a function to read them.
//Let say you have file called variable.txt
BufferedReader br = new BufferedReader(new FileReader("variable.txt"));
try {
String line = br.readLine();
List<String> verifyItem = new ArrayList<String>();
while (line != null) {
verifyItem.add(line);
line = br.readLine();
}
} finally {
br.close();
}
//do something with verifyItem
Note: you can also change List<String> verifyItem
into HashMap<String, String> verifyItem
if you would like to access it easily.
It's up to you. It depends on the way you use verifyItem
.
I hope this answer will help you.
Because I declared 100+variables and need to add them on list.Do we have other approach to add them smarter? Any hotkey in Eclipse or Intellij?
I don't think declaring 100+ variable inside the code is a good idea. In the future, another coder who read the code will be confused with this 100 variable.
If I were you, I will create a separate file to store the 100+ variables value. Then, I'll create a function to read them.
//Let say you have file called variable.txt
BufferedReader br = new BufferedReader(new FileReader("variable.txt"));
try {
String line = br.readLine();
List<String> verifyItem = new ArrayList<String>();
while (line != null) {
verifyItem.add(line);
line = br.readLine();
}
} finally {
br.close();
}
//do something with verifyItem
Note: you can also change List<String> verifyItem
into HashMap<String, String> verifyItem
if you would like to access it easily.
It's up to you. It depends on the way you use verifyItem
.
I hope this answer will help you.
answered Nov 20 '18 at 9:52


David SetyanugrahaDavid Setyanugraha
461
461
add a comment |
add a comment |
If like to collect all the fields in same class you can do it by using java reflection, below is example
Fields class
class Fields{
String name= "hello";
int age =10;
}
Main class collect filed Names into List
and Name, values into Map
public static void main(String args) {
Fields fields = new Fields();
Field f= fields.getClass().getDeclaredFields();
List<String> fieldNames=Arrays.stream(f).map(i->i.getName()).collect(Collectors.toList());
Map<String, Object> fieldNameValues = Arrays.stream(f).
collect(Collectors.toMap(Field::getName, i->{
try {
return i.get(fields);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}));
System.out.println(fieldNames); //[name, age]
System.out.println(fieldNameValues); //{name=hello, age=10}
}
add a comment |
If like to collect all the fields in same class you can do it by using java reflection, below is example
Fields class
class Fields{
String name= "hello";
int age =10;
}
Main class collect filed Names into List
and Name, values into Map
public static void main(String args) {
Fields fields = new Fields();
Field f= fields.getClass().getDeclaredFields();
List<String> fieldNames=Arrays.stream(f).map(i->i.getName()).collect(Collectors.toList());
Map<String, Object> fieldNameValues = Arrays.stream(f).
collect(Collectors.toMap(Field::getName, i->{
try {
return i.get(fields);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}));
System.out.println(fieldNames); //[name, age]
System.out.println(fieldNameValues); //{name=hello, age=10}
}
add a comment |
If like to collect all the fields in same class you can do it by using java reflection, below is example
Fields class
class Fields{
String name= "hello";
int age =10;
}
Main class collect filed Names into List
and Name, values into Map
public static void main(String args) {
Fields fields = new Fields();
Field f= fields.getClass().getDeclaredFields();
List<String> fieldNames=Arrays.stream(f).map(i->i.getName()).collect(Collectors.toList());
Map<String, Object> fieldNameValues = Arrays.stream(f).
collect(Collectors.toMap(Field::getName, i->{
try {
return i.get(fields);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}));
System.out.println(fieldNames); //[name, age]
System.out.println(fieldNameValues); //{name=hello, age=10}
}
If like to collect all the fields in same class you can do it by using java reflection, below is example
Fields class
class Fields{
String name= "hello";
int age =10;
}
Main class collect filed Names into List
and Name, values into Map
public static void main(String args) {
Fields fields = new Fields();
Field f= fields.getClass().getDeclaredFields();
List<String> fieldNames=Arrays.stream(f).map(i->i.getName()).collect(Collectors.toList());
Map<String, Object> fieldNameValues = Arrays.stream(f).
collect(Collectors.toMap(Field::getName, i->{
try {
return i.get(fields);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}));
System.out.println(fieldNames); //[name, age]
System.out.println(fieldNameValues); //{name=hello, age=10}
}
answered Nov 20 '18 at 9:50
DeadpoolDeadpool
5,1312428
5,1312428
add a comment |
add a comment |
String values = new String {...};
ArrayList<String> list = new ArrayList<String>();
for (String temp : values) {
list.add(temp);
}
List is an interface that defines some common lists's things. I think ArrayList should suit your needs.
2
If you can add variables as an Array what is point of iterating array to create a list. You can use List<String> list = Arrays.asList(values); instead
– Gihan Saranga Siriwardhana
Nov 20 '18 at 9:36
That was an example of using ArrayList<>. Of course, if code only has variables instead of array, author can simplify this step.
– Dima Rich
Nov 20 '18 at 9:51
This code converts any array to an ArrayList and has nothing to do with the problem described in the question.
– f1sh
Nov 20 '18 at 9:56
add a comment |
String values = new String {...};
ArrayList<String> list = new ArrayList<String>();
for (String temp : values) {
list.add(temp);
}
List is an interface that defines some common lists's things. I think ArrayList should suit your needs.
2
If you can add variables as an Array what is point of iterating array to create a list. You can use List<String> list = Arrays.asList(values); instead
– Gihan Saranga Siriwardhana
Nov 20 '18 at 9:36
That was an example of using ArrayList<>. Of course, if code only has variables instead of array, author can simplify this step.
– Dima Rich
Nov 20 '18 at 9:51
This code converts any array to an ArrayList and has nothing to do with the problem described in the question.
– f1sh
Nov 20 '18 at 9:56
add a comment |
String values = new String {...};
ArrayList<String> list = new ArrayList<String>();
for (String temp : values) {
list.add(temp);
}
List is an interface that defines some common lists's things. I think ArrayList should suit your needs.
String values = new String {...};
ArrayList<String> list = new ArrayList<String>();
for (String temp : values) {
list.add(temp);
}
List is an interface that defines some common lists's things. I think ArrayList should suit your needs.
answered Nov 20 '18 at 9:30


Dima RichDima Rich
273
273
2
If you can add variables as an Array what is point of iterating array to create a list. You can use List<String> list = Arrays.asList(values); instead
– Gihan Saranga Siriwardhana
Nov 20 '18 at 9:36
That was an example of using ArrayList<>. Of course, if code only has variables instead of array, author can simplify this step.
– Dima Rich
Nov 20 '18 at 9:51
This code converts any array to an ArrayList and has nothing to do with the problem described in the question.
– f1sh
Nov 20 '18 at 9:56
add a comment |
2
If you can add variables as an Array what is point of iterating array to create a list. You can use List<String> list = Arrays.asList(values); instead
– Gihan Saranga Siriwardhana
Nov 20 '18 at 9:36
That was an example of using ArrayList<>. Of course, if code only has variables instead of array, author can simplify this step.
– Dima Rich
Nov 20 '18 at 9:51
This code converts any array to an ArrayList and has nothing to do with the problem described in the question.
– f1sh
Nov 20 '18 at 9:56
2
2
If you can add variables as an Array what is point of iterating array to create a list. You can use List<String> list = Arrays.asList(values); instead
– Gihan Saranga Siriwardhana
Nov 20 '18 at 9:36
If you can add variables as an Array what is point of iterating array to create a list. You can use List<String> list = Arrays.asList(values); instead
– Gihan Saranga Siriwardhana
Nov 20 '18 at 9:36
That was an example of using ArrayList<>. Of course, if code only has variables instead of array, author can simplify this step.
– Dima Rich
Nov 20 '18 at 9:51
That was an example of using ArrayList<>. Of course, if code only has variables instead of array, author can simplify this step.
– Dima Rich
Nov 20 '18 at 9:51
This code converts any array to an ArrayList and has nothing to do with the problem described in the question.
– f1sh
Nov 20 '18 at 9:56
This code converts any array to an ArrayList and has nothing to do with the problem described in the question.
– f1sh
Nov 20 '18 at 9:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389666%2fhow-to-add-variables-to-list-if-there-is-many-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I hardly think so. But wouldn't it be smarter to just create these variables in the list in the first place?
– Tu.ma
Nov 20 '18 at 9:15
4
Ideally you shouldn't declare 100 variables, you could've used a collection. Now to add all of them to the list, you can iterate over the original collection and add them to the list.
– Nicholas K
Nov 20 '18 at 9:16
Are the variables incremental? As in the example in you have given, is there a pattern to the variables, as you may be able to create them directly to the list in a loop
– Kleo G
Nov 20 '18 at 9:16
Have a look at this: stackoverflow.com/questions/15112590/…
– Robert Kock
Nov 20 '18 at 9:18
1
If you have 100 variables like that, it is probably not a good design. Can you tell us why you need 100+ variables?
– MC Emperor
Nov 20 '18 at 9:59