I'm working in Java on a project that requires me to make a few 'container' classes, if you will. Here is a simple version of one:
public class Pair{
Object KEY;
Object VALUE;
public Pair(Object k, Object v)
{
KEY = k;
VALUE = v;
}
public Object getKey()
{ return KEY; }
public Object getValue()
{ return VALUE; }
}
(Please note, this is severely simplified and I am using proper set/get methods in the final version.)
My question is this:
When calling the println method with an ArrayList as the parameter, for example:
ArrayList<String> arr = new ArrayList<String>();
arr.add("one");
arr.add("two");
System.out.println(arr);
Java automatically knows how to print each 'node' or element of the ArrayList correctly.
Is there a way to write a method to allow the println method to correctly print my Pair class?
public String toString()
– Suellen