Hamza
Hamza

Reputation: 23

How to iterate to a certain index in a 2D array which is inside an ArrayList?

I want to know how I can iterate to a certain index inside a 2D array, for example how could I fetch the itemQuantity in this code and if there were more than one products how could I fetch all of their quantities.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class practice2 {
    public static void main(String[] args) {
        List<String[]> inventoryTable = new ArrayList<>();
        inventoryTable.add(new String[]{
                "Item Name",
                "Item Type",
                "Quantity",
                "Price",
                "Expiry Date",
                "Date of Purchase"});

        String itemName = "oranges";
        String itemType = "fruit";
        String itemQuantity = "3";
        String itemPrice = "2";
        String itemExpiryDate = "12/11/2020";
        String itemDateOfPurchase = "30/10/2020";

        inventoryTable.add(new String[]{
                itemName,
                itemType,
                itemQuantity,
                itemPrice,
                itemExpiryDate,
                itemDateOfPurchase
        });
        System.out.println(Arrays.deepToString(inventoryTable.toArray()));
    }
}

Upvotes: 0

Views: 116

Answers (2)

mattie
mattie

Reputation: 101

You can look at the arraylist as an array. Meaning you can access the indexes [][] but would need to use the get(index)[] instead.

For example how could I fetch the itemQuantity in this code:

inventoryTable.get(1)[2];

would get you the itemQuantity.

And if there were more than one products how could I fetch all of their quantities.

for (int i = 1; i < inventoryTable.size(); i++) {
    System.out.println(inventoryTable.get(i)[2]);
}

Upvotes: 1

Kiril Dimitrov
Kiril Dimitrov

Reputation: 51

In your case, you have an ArrayList containing String arrays where the third index is the quantity you're looking for.

Using the java "foreach" statement:

//first we iterate through the whole ArrayList obtaining each inventory element
for (String[] inventoryElement : inventoryTable) {
    // then we take the third index and we print it out
    System.out.println(inventoryElement[3]);
}

Edit: As someone already mention in the comments it's much better to create an object to hold all the information instead of String array.

Example solution:

List<InventoryItem> inventoryTable = new ArrayList<>();
String itemName = "oranges";
String itemType = "fruit";
int itemQuantity = 3;
double itemPrice = 2.0;
String itemExpiryDate = "12/11/2020";
String itemDateOfPurchase = "30/10/2020";
inventoryTable.add(new InventoryItem(itemName, itemType, itemQuantity,
        itemPrice, itemExpiryDate, itemDateOfPurchase));
for (InventoryItem inventoryItem : inventoryTable) {
    System.out.println(inventoryItem.getPrice());
}
public static class InventoryItem {
    private String name, type, expiryDate, dateOfPurchase;
    private int quantity;
    private double price;

    public InventoryItem(String name, String type, int quantity,
                         double price, String expiryDate,
                         String dateOfPurchase) {
        this.name = name;
        this.type = type;
        this.quantity = quantity;
        this.price = price;
        this.expiryDate = expiryDate;
        this.dateOfPurchase = dateOfPurchase;
    }

    public String getName() { return name; }
    public double getPrice() { return price; }
    public int getQuantity() { return quantity; }
    public String getDateOfPurchase() { return dateOfPurchase; }
    public String getExpiryDate() { return expiryDate; }
    public String getType() { return type; }

    @Override
    public String toString() {
        return "Item: " + name
                + " Type: " + type
                + " Quantity: " + quantity
                + " Price:" + price
                + " Expiry date: " + expiryDate
                + " Date of purchase" + dateOfPurchase;
    }
}

Upvotes: 1

Related Questions