java - Create class object only if its parameter is not null -
i developing application in have 1 separate class socket related communication.in service class creating array of class object :
string ip1 = "192.168.1.19"; string ip2 = "192.168.1.51"; @override public void oncreate() { roomstatus = new readroomstatus[] { new readroomstatus(ip1), new readroomstatus(ip2) }; }
and have loop read status :
for(i=0;i<roomstatus.length;i++) { string status = roomstatus[i].readstatus(); log.v(tag, "status ::"+status); broadcastroomstatus(i, status); }
so loop iterate number of object array. here iterate 2 times.
i getting ip address shared preferences. if user doesn't set of ip class object should not created ip.
i.e if 1 ip set created class object should this
roomstatus = new readroomstatus[] {new readroomstatus(ip1)};
if 2 ips set
roomstatus = new readroomstatus[] {new readroomstatus(ip1),new readroomstatus(ip2};
so initial goal if of ip null object shouldn't initialize ip , loop should iterate no of times ips available.
how achieve this? if have other idea please suggest me. , advice appreciated.
thanks
update
readroomstatus roomstatus1=new readroomstatus(ip1); readroomstatus roomstatus2=new readroomstatus(ip2); aliststatus = new arraylist<readroomstatus>(); if(ip1!=""){ log.v(tag, "ip1 not null"); aliststatus.add(roomstatus1); } if(ip2!=""){ log.v(tag, "ip2 not null"); aliststatus.add(roomstatus2); }
don't use array. use arraylist, list of objects , who's size can changed @ time. loop through ip addresses, , if not null add() arraylist.
Comments
Post a Comment