Java: Print elements of User defined Classes using ArrayList

Viewed 49

When I try to run the code I don't get 101,102,103 as output rather I get memory addresses of each value, I want to print the values 101,102,103 using "user defined class" pt_5_Employee as a parameter to the Array List . I tried using both for-loop and for-each-loop, but it dosen't seem to be working. Will be grateful to any help to fix this problem :) .

CODE:

    public class pt_5_employee {
        private int empId;
        public pt_5_employee(int empId) {
            this.empId=empId;
        }
        public static void main(String[] args) {
            ArrayList<pt_5_employee> x = new ArrayList<pt_5_employee>();
            x.add(new pt_5_employee(101));
            x.add(new pt_5_employee(102));
            x.add(new pt_5_employee(103));
            
            for(pt_5_employee X:x) {
                System.out.println(X);
            }
            
            System.out.println("-------\n");
            
            for(int i=0;i<x.size();i++) {
                System.out.println(x.get(i));
            }
            
        }
    }

Output which I'm getting:

Lists.pt_5_employee@6f2b958e
Lists.pt_5_employee@5e91993f
Lists.pt_5_employee@1c4af82c
-------

Lists.pt_5_employee@6f2b958e
Lists.pt_5_employee@5e91993f
Lists.pt_5_employee@1c4af82c
2 Answers

x. Get (i) gets an object, that is, pt_ 5_ If the employee instance is printed directly, it must be an object address, so you need to change x.get (i) to x.get (i). empId to print the results you want.

In your code you are trying to print the class reference generated by the default toString() method in the Class.class. The documentation states:

Converts the object to a string. The string representation is the string "class" or "interface", followed by a space, and then by the fully qualified name of the class in the format returned by getName. If this Class object represents a primitive type, this method returns the name of the primitive type. If this Class object represents void this method returns "void".

Returns: a string representation of this class object.

For what you want to achieve, you can either override the toString() method.

In this case your code would look like this:

public class pt_5_employee {
        private int empId;
        public pt_5_employee(int empId) {
            this.empId=empId;
        }
    //Here you override the toString() method
    @Override
    public String toString(){
        return (String.valueOf(empId));
    }
        public static void main(String[] args) {
            ArrayList<pt_5_employee> x = new ArrayList<pt_5_employee>();
            x.add(new pt_5_employee(101));
            x.add(new pt_5_employee(102));
            x.add(new pt_5_employee(103));
            
            for(pt_5_employee X:x) {
                System.out.println(X);
            }
            
            System.out.println("-------\n");
            
            for(int i=0;i<x.size();i++) {
                System.out.println(x.get(i));
            }
            
        }
    }

...Or create a getter for empId:

public class pt_5_employee {
        private int empId;
        public pt_5_employee(int empId) {
            this.empId=empId;
        }
        //This is the getter method for empId
        private int getEmpId(){
            return empId
        }        
        
        public static void main(String[] args) {
            ArrayList<pt_5_employee> x = new ArrayList<pt_5_employee>();
            x.add(new pt_5_employee(101));
            x.add(new pt_5_employee(102));
            x.add(new pt_5_employee(103));
            
            for(pt_5_employee X:x) {
                System.out.println(X.getEmpId()); //You are calling the getter method when printing in the console
            }
            
            System.out.println("-------\n");
            
            for(int i=0;i<x.size();i++) {
                System.out.println(x.get(i).getEmpId);//Same here
            }
            
        }
    }
Related