JAVA - How to print the attributes you assign in my objects?

2

I'm using Iterator with its respective library, compile, but it throws this result:

  

COSTS @ 940a5a93 COSTS @ 940a5a93

This is my code:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class ListaConcepto {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<COSTOS> Lista = new ArrayList<COSTOS>();

        COSTOS obj1 = new COSTOS(1781, 359.13, "BISAG.SUP.PUER.TRA.I", "67550T9AT00ZZ");
        COSTOS obj2 = new COSTOS(1781, 359.13, "BISAG.SUP.PUER.TRA.I", "67550T9AT00ZZ");

        Lista.add(obj1);
        Lista.add(obj2);


            System.out.println("¿Son iguales? " + obj1.equals(obj2));

            Iterator iter = Lista.iterator();
            while (iter.hasNext())
              System.out.println(iter.next());


    }

}


public class COSTOS  {
        private int referencia;
        private double monto;
        private String descripcion;
        private String NumeroParte;


        public COSTOS(int referencia, double monto, String descripcion, String numeroParte) {
         this.referencia = referencia;
         this.monto = monto;
         this.descripcion = descripcion;
         this.NumeroParte = numeroParte;
        }

//getters and  setters

This is my class code, what could you be doing or what is wrong or what I need to implement?

    
asked by Antonio Alejos 22.10.2018 в 15:05
source

2 answers

6

When you use

System.out.println(loquesea);

What that method does is (conceptually):

if (loquesea instanceof String) {
    print(loquesea + "\n");
} else {
    print(loquesea.toString() + "\n");
}

All objects, by extending the class Object , have the toString () method already implemented, but unless you overwrite it will only show the name of the class and code associated with the memory address where it is (a kind of identifier).

Therefore, the solution is as simple as having your COSTS class implement this method, showing what you want to be displayed, for example:

public class COSTOS  {
    private int referencia;
    private double monto;
    private String descripcion;
    private String NumeroParte;




    public String toString() {
        return this.referencia + " - " + this.monto + " (" + this.descripcion +")";
    }

    ...
}
    
answered by 22.10.2018 / 15:27
source
-6

You have to put Arraylist list = new Arraylist ();

You can not start List List = new Arraylist, this does not make sense.

If you use List you are calling the List class or if you use ArrayList you are calling the ArrayList class, but what you have put in makes no sense.

A part, in the iterator should tell you that you want to take, for example, in the system that you take the amount, you would use it.getMonto () and return the amount of each iterator object, that system so you only is going to get that information that appears, you have to use the getters that you have created to get data.

    
answered by 22.10.2018 в 15:13