java - Converting List<Object> to String returns empty results -


disclaimer : i'm using this post, reference list<object> list<string> , this post java list<string> of strings javascript array.

i've list<seat> , want values of in comma separated string, tried in way

import java.util.*; import java.lang.*;  class rextester {        public rextester(){         seat seat1 = new seat();         seat1.setseatnumber(1);          seat seat2 = new seat();         seat2.setseatnumber(2);          seat seat3 = new seat();         seat3.setseatnumber(3);           list<seat> seatlist = new arraylist<seat>();         seatlist.add(seat1);         seatlist.add(seat2);         seatlist.add(seat3);         utility util = new utility();         string stringseats = util.tojavascriptarray(seatlist);         system.out.println("javascriptarray " + stringseats);     }      public static void main(string args[])     {         new rextester();      }        private class seat {          private integer seatnumber;                  public integer getseatnumber() {             return this.seatnumber;         }          public void setseatnumber(integer seatnumber) {             this.seatnumber = seatnumber;         }            public string tostring() {             return ""+ seatnumber;         }     }      private class utility {          public string tojavascriptarray(list<seat> listseats){             list<string> strings = new arraylist<string>();             (seat object : listseats) {                 strings.add(object != null ? object.tostring() : null);             }             string[] arr = new string[strings.size()];             arr = strings.toarray(arr);             stringbuffer sb = new stringbuffer();             sb.append("[");             for(int i=0; i<arr.length; i++){                  if(i+1 < arr.length){                     sb.append(",");                 }             }             sb.append("]");             return sb.tostring();         }     } } 

but gives me

javascriptarray [,,] 

on console, making mistakes? online working code http://rextester.com/ndugt61105

you didn't append iterated element, see below

        (int = 0; < arr.length; i++) {             sb.append(arr[i]); // add             if (i + 1 < arr.length) {                 sb.append(",");             }         } 

have @ tostring implementations of arrays , list

simply can return

strings.tostring() 

or

arrays.tostring(arr) 

to expected result


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -