java - Print a set collection -
i'm studying collections right , learned saw set type of collection don't permit duplicate elements. ok, i've created class adds 3 int numbers collection. seems ok question is: how print collection? know override string method since elements integer type how that? expected output be: 2,3,2 4,5,6
the code (adding numbers)
public class adaugarenumere { int c=0; int f=0; int r=0; adaugarenumere(int c, int f, int r){ this.c=c; this.f=f; this.r=r; } }
main class:
import java.util.*; public class executare { public static void main(string[] args) { adaugarenumere primulrand=new adaugarenumere(2,3,2); adaugarenumere aldoilearand=new adaugarenumere(2,3,2); adaugarenumere altreilearand=new adaugarenumere(4,5,6); set<adaugarenumere> lista=new hashset<adaugarenumere>(); lista.add(primulrand); lista.add(aldoilearand); lista.add(altreilearand); system.out.println("elementele listei: "+arrays.aslist(lista.tostring())); } }
you need override tostring()
method in adaugarenumere
class.
example:
@override public string tostring() { return "adaugarenumere [c=" + c + ", f=" + f + ", r=" + r + "]"; }
and give list in sop statement.
system.out.println("elementele listei: " + lista); // no need `arrays.aslist()` or `tostring()`.
Comments
Post a Comment