简体   繁体   中英

How can i split a string in an ArrayList in JAVA

How can i split a String into an ArrayList in java? I am reading the String

st = input.nextLine();

and i want to split it in my ArrayList

list.add(st);

假设comma(,)是您的定界符,则可以使用

new ArrayList<String>(Arrays.asList( str.split(",") ));

First Way:

String str = "test,abc,123,xyz,win,hk";
List<String> list = new ArrayList<String>(Arrays.asList(str.split(",")));
System.out.println(list);

Second Way:

List<String> list1 = new ArrayList<String>();
  for(int win = 0; win < str.split(",").length; win++ ){
    list1.add(str.split(",")[win]);
  }
System.out.println(list1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM