0

I have the below method which will return string.

  private String getExpectedList() {
        return expectedSList;
    }

so i am storing it in string like as shown below

String t = this.getExpectedList();

upon debugging i found that method inside t the string is like comma separated as shown below

System.out.println(t);
  bonrs01721.am.grp.net:17202,bonrs01422.am.grp.net:17203,bonrs01622.am.grp.net:17204

i was thinking to design a method which will take the string and then split each of them and finally add it in list and the list will be of type string so finally a method of which return type is list

so i have designed a list

  List<String> holdvalues = new List<String>();

how can i design such a method as mentioned above

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

You can do:

public List<String> method(String s){

    // create a list

    List <String>myList=new ArrayList<String>();
    String[] array;

    // split the string and store it in the array

    array=s.split("[,]");
    for(int i=0;i<array.length;i++){

        // store the strings in the list

        myList.add(array[i]);
    }

    //return the list

    return myList;
}
hermit
  • 1,048
  • 1
  • 6
  • 16
0

try :

 List<String> holdvalues = new ArrayList<>();
 holdvalues.addAll( Arrays.asList(t.split(",")));
 System.out.println(holdvalues);
Rustam
  • 6,485
  • 1
  • 25
  • 25