Access to arraylist fields individually

1

I have a class called Product () which has the following form:

public class Producto {

private String nombre;
private float precio;
private float iva;
private int unidades;

public Producto(String n, float p, float i, int u){

    this.nombre = n;
    this.precio = p;
    this.iva = i;
    this.unidades = u;
}

And I want to create a method through which I can access and process the different fields of the Product. I have created an arrayList on product in which is stored the information of the products that are imported from a text file, which are a product by line and name, price, VAT and units separated by #.

private ArrayList<Producto> productosImportados;

The problem is that this arraylist keeps the products for me, but the entire product in each cell, that is, in cell i, is everything related to that product and I can not treat its fields individually. Nor can I access products Imported.element (i) [j]. Any solution? Thanks.

    
asked by Mario Hernandez 12.12.2017 в 20:00
source

3 answers

1

You can create the getter and setter (Encapsulate) to your product class and call your product according to its position in the arraylist with myArrayList.get(index); or get the product id with myArrayList.indexOf(objetoProducto); .

Once you get your id you only use myArrayList.set(id, objetoProductoModificado); and you update the values in your ArrayList

    
answered by 12.12.2017 / 20:04
source
1

You only have the constructor of the class, you lack the methods to access the attributes of it, you have to create them. It sounds like a programming class exercise. You have to work on it.

    
answered by 12.12.2017 в 20:05
1

Is it something you're looking for?

for (int i = 0; i<productos.size(); i++){
  System.out.println(productos.get(i).getNombre());
        if(productosImportados.get(i).getPrecio == x){
            //Lo que sea
        }
}
    
answered by 12.12.2017 в 21:39