-2

I am simply trying to print an ArrayList using a name class I made.

Here is the method

private static ArrayList createActorList() {
    ArrayList<Name> actors = new ArrayList<>();
    actors.add(new Name("Tom","Cruise"));
    actors.add(new Name("Sylvester","Stallone"));
    actors.add(new Name("Michael J.","Fox"));
    actors.add(new Name("Arnold","Schwarzenegger"));
    actors.add(new Name("Al","Pacino"));
    actors.add(new Name("Harrison","Ford"));
    actors.add(new Name("Matt","Dillon"));
    actors.add(new Name("Michael","Biehn"));
    actors.add(new Name("Emilio","Estevez"));
    actors.add(new Name("Rob","Lowe"));
    return actors;
}

Name class-The name class works fine for other programs printing the name when called.

    public class Name implements NameADT{
        public String first;
        public String last;
        public String natural;
        public String formal;
        public String check;

   public Name(String firstName, String lastName) {
    first = firstName;
    last = lastName;

}

@Override
public String first() {
    return first;
}

@Override
public String last() {
    return last;
}

@Override
public String natural() {
    natural = first + " " + last;
    return natural;
}

@Override
public String formal() {
    formal = last + ", " + first;
    return formal;
}

Output when I print

The actors ...

simplethings.Name@7852e922<br>
simplethings.Name@4e25154f<br>
simplethings.Name@70dea4e<br>
simplethings.Name@5c647e05<br>
simplethings.Name@33909752<br>
simplethings.Name@55f96302<br>
simplethings.Name@3d4eac69<br>
simplethings.Name@42a57993<br>
simplethings.Name@75b84c92<br>
simplethings.Name@6bc7c054<br>
Zhi Kai
  • 1,549
  • 1
  • 13
  • 31

2 Answers2

-1

Your problem lies in how Java stores objects (non-primitive data types). In short, a variable that does not store a primitive data type (ints, doubles, floats, etc) only hold the address to where the actual data is stored. This means the variable only holds a reference to the object in memory, not the object itself.

That is what you are seeing when you try to print out the Name object (the address in your computer's memory where the actual instance is stored).

To solve this, you can override the toString() method of Object, which is implicitly called to convert to object to a string for printing. In your Name class, override it and return your first and last name:

@Override
public String toString() {
    return first + " " + last;
}
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
kbunarjo
  • 1,277
  • 2
  • 11
  • 27
-2

The problem is because you are trying to print the object as it is. try using a list iterator and print the names. something like :

ListIterator<String> itr=al.listIterator();  
while(itr.hasNext()){ \\traversing elements in forward direction...
System.out.println(itr.next());  
 }  
Shree Naath
  • 477
  • 2
  • 5
  • 18
  • A better and more conventional way to solve this is to override the `toString` method of the `Name` object... this solution isn't very optimal and isn't fundamentally the best and encouraged practice – Andrew Li Oct 16 '16 at 05:44