Javanoob33
Javanoob33

Reputation: 89

arraylist returning random numbers and not proper data

so I've got an arraylist

private static ArrayList<Job> teamNoOne = new ArrayList<Job>();

Of type Job, which has

public class Job {
    //public long time;
    public int teamNo;
    public String regNo;
    public String gridRef;

}

Then I'm getting data from a textField

Job job = new Job();
job.gridRef = tfGridRef.getText();

This all works, printed it to system etc.

When I add it to the arraylist, and print it out using the following code:

teamNoOne.add(job);

System.out.println(teamNoOne.get(0));

I just get this: Job@1c79dfc

cannot for the life of me figure out why,

Cheers

Upvotes: 0

Views: 62

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

When a object is printed using sysout, its toString method is called. If toString method is not overridden in the class, then its default implementation will be used. Default implementation of an object toString method prints the class name and the unsigned hexadecimal representation of the hash code of the object separated by @ symbol.

You need to override the toString method in your Job class in order to print the object in a pretty way. Most of the IDEs provide a way to auto- generate the toString method. Here is one generated through eclipse:

@Override
public String toString() {
    return "Job [teamNo=" + teamNo + ", regNo=" + regNo + ", gridRef="
            + gridRef + "]";
}

Upvotes: 4

Related Questions