-1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class clsWarehouse {

private static int iChoice;
private int iItemID;
private String strItemName;
private String strItemDescription;
private int iItemPrice;
private String strSize;
private String strSex;


public clsWarehouse (int id, String name, String description, int price, String size, String sex){
    iItemID = id;
    strItemName = name;
    strItemDescription = description;
    iItemPrice = price;
    strSize = size;
    strSex = sex;   
}

public ArrayList <clsWarehouse> AllItems;
    public clsWarehouse(){
        AllItems = new ArrayList <clsWarehouse>();  
        AllItems.add(new clsWarehouse(3, "T-Shirt", "Male T-Shirt", 30, "15", "Male"));
        System.out.println(AllItems);
    }

public static void main(String[] args){
    clsWarehouse c = new clsWarehouse();
    }
}
 }

Hello, I want to get the values stored in the ArrayList AllItems but all i get is the memory location [clsWarehouse@a83b8a]. Can someone help me with this.

Thanks!

user207421
  • 305,947
  • 44
  • 307
  • 483
user2260906
  • 9
  • 1
  • 1
  • 1

3 Answers3

3

You need to override the toString() in your clsWarehouse class. The System.out.println internally calls the toString() of the object in the list. Since you do not have the toString() method overridden in your class, you get the below as your output.

getClass().getName() + '@' + Integer.toHexString(hashCode())

The above is the toString() implementation of the Object class.

Update:-

In your clsWarehouse class, add the below method.

public String toString(){
    return  "clsWarehouse"; //Modify this to return whatever you want to display in your sysout.
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

You need to iterate through the array list to get the values . Also need to convert this guy to string format using toString(). Then,

 for (int i=0;i<AllItems .length();i++){
 //Then get the values one by one
 }

Hope this helps you .

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
0

Add this method in clsWarehouse class

public String toString(){
    return  "iItemID: " + iItemID +" strItemName: "+ strItemName + " strItemDescription: "+ strItemDescription +" iItemPrice  "+ iItemPrice: + " strSize: " + strSize + " strSex: "+strSex; 
}

Change this

System.out.println(AllItems);

to

for(clsWarehouse item : AllItems){
     System.out.println(item.toString());
}
Kishore
  • 819
  • 9
  • 20