Add multiple String variables to ArrayList

前端 未结 6 2093
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 18:24

Suppose I have a lot of String Variables(100 for example):

   String str1 = \"abc\";
    String str2 = \"123\";
    String str3 = \"aaa\";
....
    String st         


        
6条回答
  •  -上瘾入骨i
    2021-01-14 18:38

    Yes. The way to use a loop is not to declare 100 string variables. Use one array instead.

    String[] str = new String[101];
    str[1] = "abc";
    str[2] = "123";
    str[3] = "aaa";
    ....
    str[100] = "zzz";
    

    (I made the indexes go from 1 to 100 to show how it corresponds to your original code, but it's more normal to go from 0 to 99 instead, and to initialize it with an array initializer as in @markspace's answer.)

提交回复
热议问题