Having Trouble Implementing toString() Method in (Java) -
Having Trouble Implementing toString() Method in (Java) -
i writing programme uses 2 different classes. 1 class contains main method , other class generic class set has set of items of generic type t uses arraylist.
this programme finds intersection of 2 sets.
i want utilize tostring() method don't know how implement in specific situation when info fields not visible.
import java.util.random; //generic class set<t> class set <t> { arraylist<t> num = new arraylist<t>(); /*within class have (1) add together method, (2) remove method, , (3) method returns true if item in set , false if not in set*/ //this intersection method public static <t> set<t> intersection(set<t> k, set<t> p){ set<t> abc = new set<t> (); /*i have other codes here find intersection of 2 different sets*/ homecoming abc; } @override /*here lost not know how utilize method in order print out intersection of both sets*/ public string tostring() { /*i don't know implement here in order homecoming string represents current object*/ return; } } public class secondclass { //main method public static void main(string [] args){ /* programme generates random numbers 2 sets in order find intersection of both sets. */ set<integer> firstset = new set<integer>(); set<integer> secondset = new set<integer>(); set<integer> result = new set<integer>(); result = set.intersection(firstset,secondset); //display intersection? system.out.println(result.tostring()); } }
it seems using arraylist
backing info structure. has well-implemented tostring()
, why not delegate it?
@override public string tostring() { homecoming num.tostring(); }
java tostring
Comments
Post a Comment