0

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.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • 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 09: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 09: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 09:16
  • Have a look at this: https://stackoverflow.com/questions/15112590/get-the-class-instance-variables-and-print-their-values-using-reflection – Robert Kock Nov 20 '18 at 09:18
  • but please don't use it ^ – Tim Nov 20 '18 at 09:22
  • Do you need the variables or only the list? – f1sh Nov 20 '18 at 09:25
  • You should adhere to the Java Naming Conventions: variable names start with lowercase. I've edited the code in your post for you. – MC Emperor Nov 20 '18 at 09:40
  • 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 09:59
  • Thanks for you guys. Actually there are ~20 variables only. The variables are fields in registration form like firstname,lastname,nationalid,mobile number, address.....etc around 20 fields. I am using selenium with framework for automation testing. That framework can get data from excel sheet. So I need to declare variables like String firstName=excel.getdata("firstname"); 20times. And then put them to list so I can write a loop to check all 20 fields are displayed correctly in web page. – sgsdgdsgsd Nov 21 '18 at 03:31
  • Why don't you save directly to map in key value pair, instead of variables – Ryuzaki L Nov 21 '18 at 14:03

1 Answers1

1

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.