-1

Now, I want it to only print what it says, without the memory address as well. How would I achieve that?

    public Telefonnummer[] getTelenummer() {

    Telefonnummer[] tnummer = new Telefonnummer[nummerarray.size()];
    nummerarray.toArray(tnummer);

    System.out.println(Arrays.toString(tnummer) );


    return tnummer;

}

Is the constructor and:

    private static void kundSök() {
    System.out.println("...? ");
    String namn = keyboard.nextLine();

    if (kunderna.containsKey(namn)) {
        for (String k : kunderna.keySet()) {
            Kund kund = kunderna.get(k);
            System.out.println(kund);
            System.out.println(kund.getTelenummer());

After i have added a person to the ArrayList etc it gives me an output of:

Sam wasdfgn [123456: efdg] [LTelefonnummer;@28d93b30

The last part, memory address bit, is the part I want to get rid of. Yet again, how do i achieve that?

Edit: I tried to Override, but it did not do anything at all. Could there be another problem?

2 Answers2

1

The default behaviour for toString is to print the type name (as L followed by the type name), followed by @ and the hexString of the hashCode (which by default is the memory address for the object).

To change this, override the toString method for your Telefonnummer class.

public class Telefonnummer {
  private String nummer;
  ...
  @Override public String toString() {
    return "Dial " + nummer + " for a good time";
  }
}
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
  • The idea that hashCode uses the memory address is a historical artefact http://stackoverflow.com/questions/36236615/does-object-tostring-or-object-hashcode-ever-give-the-memory-address-of-the-obje – Raedwald Mar 26 '16 at 17:03
0

Guava library has Joiner which can be used for that. See https://code.google.com/p/guava-libraries/wiki/StringsExplained

String str = Joiner.on(",").join(list);

You also have to have working toString function on class for elements of the list

ikettu
  • 1,203
  • 12
  • 17