Converts a set or array to a String string.
1. Convert the set to a String
String s="";for (int i = 0; i < numList.size(); i++) { if (s=="") {s=numList.get(i); }else {s=s+","+numList.get(i); }}
- Define a List set, for example:
List<String> numList=new ArrayList<String>();for(int i=1; i<10; i++){ numList.add(String.valueOf(i)); }
- Define String s = "" to cyclically store the value retrieved from numList.
- Output result (with "," in the middle of the character) such as: s =, 1, 2, 3, 4, 5, 6, 7, 8, 9
- Output result (no interval between characters) such as: s = 123456789
- String. valueOf () This method can convert a non-String value to a String value.
2. Convert the array to a String.
String [] arr = {"abc", "123", "@#&"};StringBuffer sb = new StringBuffer();for(int i = 0; i < arr.length; i++){sb. append(arr[i]);}String ns = sb.toString();System.out.println(ns);}
- The displayed result is abc123 @#&
- You can use the String. valueOf () method to convert a non-String value to a String value. An int array is also available. Example: sb. append (String. valueOf (arr [I]);