如何在数组对象上调用获取方法-Java

发布于 2025-01-23 18:55:11 字数 1604 浏览 0 评论 0原文

我有2个类,在Sumusercart中,我初始化对象的数组,然后我必须在Productcart中调用productid in ProductiD的方法,任何解决方案吗?

数据位于列表

List<SumUserCart> cartList

Sumusercart类中:

public class SumUserCart {
    int userId;
    ProductsCart[] products;

    public SumUserCart(int userId, ProductsCart[] products) {
        this.userId = userId;
        this.products = products;
    }

    @Override
    public String toString() {
        return "SumUserCart{" +
                "userId=" + userId +
                ", products=" + Arrays.toString(products) +
                '}';
    }

    public ProductsCart[] getProducts() {
        return products;
    }
 
   //getters & setters here
}

Productcart类:

public class ProductsCart {
    int productId;
    int quantity;


    @Override
    public String toString() {
        return "ProductsCart{" +
                "productId=" + productId +
                ", quantity=" + quantity +
                '}';
    }

    public ProductsCart() {}

    public ProductsCart(int productId, int quantity) {
        this.productId = productId;
        this.quantity = quantity;
    }

    //getters & setters here
}

这是我的功能,我试图打印产品ID的位置:

private static void printTheCart(List<SumUserCart> cartList){
        for (SumUserCart cartsByUser : cartList) {
            System.out.println("User Id: " + cartsByUser.getUserId() + " = " + Arrays.toString(cartsByUser.getProducts()));
            //System.out.println(cartsByUser.getProducts());
        }

    }

I have 2 classes, in SumUserCart i initialize the array of object, then i have to call get method on productID in ProductCart, any solutions?

The data is in list

List<SumUserCart> cartList

SumUserCart class:

public class SumUserCart {
    int userId;
    ProductsCart[] products;

    public SumUserCart(int userId, ProductsCart[] products) {
        this.userId = userId;
        this.products = products;
    }

    @Override
    public String toString() {
        return "SumUserCart{" +
                "userId=" + userId +
                ", products=" + Arrays.toString(products) +
                '}';
    }

    public ProductsCart[] getProducts() {
        return products;
    }
 
   //getters & setters here
}

ProductCart class:

public class ProductsCart {
    int productId;
    int quantity;


    @Override
    public String toString() {
        return "ProductsCart{" +
                "productId=" + productId +
                ", quantity=" + quantity +
                '}';
    }

    public ProductsCart() {}

    public ProductsCart(int productId, int quantity) {
        this.productId = productId;
        this.quantity = quantity;
    }

    //getters & setters here
}

That's my function, where i am trying to print the product id's:

private static void printTheCart(List<SumUserCart> cartList){
        for (SumUserCart cartsByUser : cartList) {
            System.out.println("User Id: " + cartsByUser.getUserId() + " = " + Arrays.toString(cartsByUser.getProducts()));
            //System.out.println(cartsByUser.getProducts());
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多像笑话 2025-01-30 18:55:11

与数组的方式一样!使用索引值访问。
由于您想从ArrayList访问以下代码:

//get 0th element of cartList, which will be a SumUserCart object
SumUserCart suc = cartList.get(0);
//creating productsCarts object array:
ProductsCarts[] pc = suc.getProducts();
//Now use getProductId() method:
int pid = pc[0].getProductId(0);

The same way as it is done with arrays! Accessing using index values.
Since you want to access from ArrayList use below code:

//get 0th element of cartList, which will be a SumUserCart object
SumUserCart suc = cartList.get(0);
//creating productsCarts object array:
ProductsCarts[] pc = suc.getProducts();
//Now use getProductId() method:
int pid = pc[0].getProductId(0);
北城半夏 2025-01-30 18:55:11

那是称呼这个的正确方法

private static void printTheCart(List<SumUserCart> cartList){
        for (SumUserCart cartsByUser : cartList) {
            System.out.println("User Id: " + cartsByUser.getUserId() + " = " + Arrays.toString(cartsByUser.getProducts()));
            ProductsCart[] pcx = cartsByUser.getProducts();
            for (ProductsCart pc : pcx)
                System.out.println(pc.getProductId());
                
        }

That's the right method to call this

private static void printTheCart(List<SumUserCart> cartList){
        for (SumUserCart cartsByUser : cartList) {
            System.out.println("User Id: " + cartsByUser.getUserId() + " = " + Arrays.toString(cartsByUser.getProducts()));
            ProductsCart[] pcx = cartsByUser.getProducts();
            for (ProductsCart pc : pcx)
                System.out.println(pc.getProductId());
                
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文