java - How can I print out an arraylist and also How can I add errors checks to the arraylist? -
i have been trying solve problem ages , no luck didn't progress. please me out. have created arraylist, made getter class, have made method. can add stuff array list when print arraylist out prints out random text.
below arraylist created.
public static arraylist<getarraylist> arraylist = new arraylist<getarraylist>();
here method;
private static void addtoarraylist(string a, double no1, int no2, int no3) { try { arraylist.add(new getarraylist(a, no1, no2, no3)); } catch (exception e) { e.printstacktrace(); } }
here getter class
public class getarraylist { private string name; private double seconds; private int speed1; private int speed2; public string getname() { return name; } public double getseconds() { return seconds; } public int getspeed1() { return speed1; } public int getspeed2() { return speed2; } public storecommands(string storename, double storeseconds, int storespeed1, int storespeed2) throws exception{ name = storename; seconds = storeseconds; speed1 = storespeed1; speed2 = storespeed2; } }
to add stuff on list use method created
addtoarraylist(string a, double no1, int no2, int no3) filled in values
and receive stuff arraylist use this
for(getarraylist s : arraylist) { system.out.println(arraylist + "\n") ;
and prints out if use system.out.println(arraylist), depending on how add arraylist.
[storecommands@49a21b63]
besides tell me how can set size of arraylist if more being added won't add , give error. need perform error checks once items in arraylist *1st if item added list , user tries add same 1 again straight after want display error.. (the user can add same stuff not directly after 1 added, need add else first) *2nd if user wants add apples list, want limit 2 time in whole list, more not added , display , error.
could me out please, appreciate it.
thanks.
try -
for(getarraylist s : arraylist) system.out.println(s + "\n");//this print tostring of getarraylist class
again override tostring method of getarraylist
class, print actual field value of object. example -
public class getarraylist { public string tostring(){ return name +"||" +seconds+"||"+ speed1+"||"+speed2; } }
note : follow java nomenclature standard, first later of class name capital , give relevent name class.
Comments
Post a Comment