Add multiple String variables to ArrayList

前端 未结 6 2092
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  再見小時候
    2021-01-14 18:38

    The following creates the ArrayList on the specific String values you have:

    ArrayList list1 = new ArrayList() {{addAll(Arrays.asList(new String[]{"99", "bb", "zz"}));}};
    

    Or, if it's just some distinct values you want, use this for say - 10 of them:

    ArrayList list2 = new ArrayList() {{for (int i=0; i<10; i++) add(""+System.currentTimeMillis());}};
    

提交回复
热议问题