Advertisement
Guest User

Untitled

a guest
Aug 31st, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. for (String s : arrayListWords) {
  2. System.out.print(s + ", ");
  3. }
  4.  
  5. if (arrayListWords.length >= 1) {
  6. System.out.print(arrayListWords[0];
  7. }
  8.  
  9. // note that i starts at 1, since we already printed the element at index 0
  10. for (int i = 1; i < arrayListWords.length, i++) {
  11. System.out.print(", " + arrayListWords[i]);
  12. }
  13.  
  14. // assume String
  15. Iterator<String> it = arrayListWords.iterator();
  16. if (it.hasNext()) {
  17. System.out.print(it.next());
  18. }
  19. while (it.hasNext()) {
  20. System.out.print(", " + it.next());
  21. }
  22.  
  23. public static void main(String[] args) throws Exception {
  24. final List<String> words = Arrays.asList(new String[]{"one", "two", "three", "four"});
  25.  
  26. final Iterator<String> wordIter = words.iterator();
  27. final StringBuilder out = new StringBuilder();
  28. while (wordIter.hasNext()) {
  29. out.append(wordIter.next());
  30. if (wordIter.hasNext()) {
  31. out.append(",");
  32. }
  33. }
  34. System.out.println(out.toString());
  35. }
  36.  
  37. public static void main(String[] args) throws Exception {
  38. final List<String> words = Arrays.asList(new String[]{"one", "two", "three", "four"});
  39. System.out.println(Joiner.on(",").join(words));
  40. }
  41.  
  42. List<String> listWords= Arrays.asList(arrayListWords); // convert array to List
  43. StringBuilder sb=new StringBuilder();
  44. sb.append(listWords);
  45. System.out.println(sb.toString().replaceAll("\[|\]",""));
  46.  
  47. StringBuilder res = new StringBuilder();
  48. for (String s : arrayListWords) {
  49. res.append(s).append(", ");
  50. }
  51. System.out.println(res.deleteCharAt(res.length()-2).toString());
  52.  
  53. String str = java.util.Arrays.toString(arrayListWords);
  54. str = str.substring(1,str.length()-1);
  55. System.out.println(str);
  56.  
  57. String s = arrayListWords.toString();
  58. System.out.println(s);
  59.  
  60. //This will print it like this: "[one, two, three, four]"
  61. //If you want to remove the brackets you can do so easily. Just change the way you print.
  62.  
  63. String s=arrayListWords.toString();
  64. System.out.print(s.substring(1, s.length()-1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement