Question

I am attempting to construct a list of rows which contain cells. The classes row and cell have been defined.

I want to construct the array list and then print it.

My Code:

import java.util.*;

public class findBfs
{
int numWarehouses;
int numCustomers = 4;


ArrayList<Integer[]> warehouses = new ArrayList<Integer[]>();

Integer[] warehouse1 = {3,6,8,2};
Integer[] warehouse2 = {6,1,2,5};
Integer[] warehouse3 = {7,8,3,9};

class Cell {

    int cost;
    int shipment;

    public Cell(int x, int y){

        x = cost;
        y = shipment;

    }

    public int getCost(){

        return cost;
    }

    public int getShipment(){

        return shipment;
    }

    public void updateShipment(int newAmount){

        shipment = newAmount;

    }

}

class Row{

    public Row(Integer[] warehouse) {

        ArrayList<Cell> row = new ArrayList<Cell>();

        for(int value: warehouse) {

            row.add(new Cell(value, 0));

        }
    }
}

ArrayList<Row> tableu = new ArrayList<Row>();

public findBfs()
{
   warehouses.add(warehouse1);
   warehouses.add(warehouse2);
   warehouses.add(warehouse3);


    for(Integer[] thisWarehouse : warehouses)
    {
       tableu.add(new Row(thisWarehouse));
    }
}

public void printTableu() {

    for(Row thisRow : tableu) {
       System.out.println(thisRow);
    }
}
}

Currently with the code I have the following gets printed:

findBfs$Row@25ca623f
findBfs$Row@9f8297b
findBfs$Row@36b4f5a

What has happened? :(

Was it helpful?

Solution

  1. You have declared row ArrayList<Cell> type as local to the constructor. Declare it in your class context: which is probably you are wanting.
  2. override the toString() method and provide an implementation in the Row class with proper format you want it to see as String. You will probably need to override toString() method and provide an implementation for the Cell class too.
  3. For a List of specific element, i prefer to declare the list with the element name as prefix and list as suffix: suppose instead of ArrayList<Cell>row, declare it as ArrayList<Cell>cellList

For example:

class Cell {
    // your other code

    @Override
    public void String toString() {
        return "shipment: " + shipment + "; cost: " + cost;
    }
}

class Row{
    ArrayList<Cell> cellList = new ArrayList<Cell>();

    public Row(Integer[] warehouse) {

        for(int value: warehouse) 
            cellList.add(new Cell(value, 0));

    }

  @Override
    public void String toString() {
        return cellList.toString();
    }

}

Edit:

you are assigning the class variable wrongly, constructor_local_var <- class_var: which should be class_var <-- constructor_local_var

public Cell(int x, int y){

        cost = x;
        shipment = y;

    }

OTHER TIPS

class Row{

    ArrayList<Cell> row = new ArrayList<Cell>();

    public Row(Integer[] warehouse) {

        for(int value: warehouse) {

            row.add(new Cell(value, 0));

        }
    }
}

i think doing this and overriding toSting() will help

When an object is printed out, it is converted to a String first, using the toString() method. Since you did not override it, you're getting the default implementation from Object, which, as you can see, is pretty useless.

You should just override it, like you override any other method. E.g.:

class Cell {
    // snipped

    @Override
    public void String toString() {
        return "Cell [shipment: " + shipment + "; cost: " + cost + "]";
    }
}

class Row {
    // snipped

    @Override
    public void String toString() {
        // row.toString() could be simpllified to row - added for clarity.
        return "Row: " + row.toString(); 
    }
}

You need to save the warehouse or the row variable in the Row class, depending on what you want to print, and then override the toString() method.

Following is an example implementation of the Row class for the warehouse version.

class Row {

    private Integer[] warehouse;

    public Row(Integer[] warehouse) {

        this.warehouse = warehouse;

        ArrayList<Cell> row = new ArrayList<Cell>();

        for (int value : warehouse) {
            row.add(new Cell(value, 0));
        }
    }

    public String toString() {
        return Arrays.asList(this.warehouse).toString();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top