chaitu chaitu
asked on
without using iteration) can i know in the arraylist of Housedocumentcharges Object whether 620 chargeid is there or not?Is there any method is there in arraylist or in collections?
i have arraylist of Housedocumentcharges Object;in this i have different chargid's and chargesdescription will be stored.
in the arraylist if the chargeid is 620 is there i dont want to enter into UserInputModel method;
I can solve the problem using below code.but without using for loop(or using iteration) can i know in the arraylist of Housedocumentcharges Object whether 620 chargeid is there or not?Is there any method is there in arraylist or in collections?
if(chargesList1!=null && chargesList1.size()>0)
{
for(int i=0;i<chargesList1.size(); i++)
{
houseDocumentCharges = (HouseDocumentCharges)char gesList1.g et(i);
if(houseDocumentCharges.ch argeId.equ als("620") )
continue;
userInputModel = new UserInputModel(vndrList,"v endorIdLab el", ValidationConstants.MULTI_ VENDOR_VAL IDATION_NA ME,4);
validationModel.addUserInp ut("Vendor (s)",userI nputModel) ;
}
}
in the arraylist if the chargeid is 620 is there i dont want to enter into UserInputModel method;
I can solve the problem using below code.but without using for loop(or using iteration) can i know in the arraylist of Housedocumentcharges Object whether 620 chargeid is there or not?Is there any method is there in arraylist or in collections?
if(chargesList1!=null && chargesList1.size()>0)
{
for(int i=0;i<chargesList1.size();
{
houseDocumentCharges = (HouseDocumentCharges)char
if(houseDocumentCharges.ch
continue;
userInputModel = new UserInputModel(vndrList,"v
validationModel.addUserInp
}
}
ArrayList has a method contains()
public boolean contains(Object elem)
Returns true if this list contains the specified element.
Parameters: elem - element whose presence in this List is to be tested.
Returns: true if the specified element is present; false otherwise.
Returns true if this list contains the specified element.
Parameters: elem - element whose presence in this List is to be tested.
Returns: true if the specified element is present; false otherwise.
if (chargesList1.contains("62 0"))
{
...
{
...
You can also use i=the indexOf() method to get the index
int index = chargesList1.indexOf("620" );
if (index==-1)
{
// not there
}
else
{
// its in list
}
int index = chargesList1.indexOf("620"
if (index==-1)
{
// not there
}
else
{
// its in list
}
So,
chargesList.contains(...)
but the parameter you pass must be the exact object you're looking for (equals() must be true)
chargesList.contains(...)
but the parameter you pass must be the exact object you're looking for (equals() must be true)
>> but the parameter you pass must be the exact object you're looking for (equals() must be true)
So, a simple
if (chargesList1.contains("62 0"))
won't work, since your arraylist doesn't contain Strings but Housedocumentcharges objects
So, a simple
if (chargesList1.contains("62
won't work, since your arraylist doesn't contain Strings but Housedocumentcharges objects
ASKER
so contains and indexOf won't work here.........any other alternative
>> so contains and indexOf won't work here.
I didn't say that.
**If** your Housedocumentcharges class has equals() & hashCode() methods
that are based on the chargeId, then passing a new Housedocumentcharges object with chargeId "620" is sufficient:
if ( chargesList1.contains( new Housedocumentcharges("620" ) )
I didn't say that.
**If** your Housedocumentcharges class has equals() & hashCode() methods
that are based on the chargeId, then passing a new Housedocumentcharges object with chargeId "620" is sufficient:
if ( chargesList1.contains( new Housedocumentcharges("620"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
we haven't implemented these two methods in HouseDocumentCharges class and also the Housedocumentcharges constructor
public boolean equals(Object obj) {
return chargeId.equals( ((Housedocumentcharges)obj ).getCharg eId() );
}
public int hashCode() {
return getChargeId().hashCode();
}
public boolean equals(Object obj) {
return chargeId.equals( ((Housedocumentcharges)obj
}
public int hashCode() {
return getChargeId().hashCode();
}
>> we haven't implemented these two methods
Well, if you want to use contains() you should ;°)
Well, if you want to use contains() you should ;°)
ASKER
we dont have get set methods in bean class only public variables defined like public String chargeId.so now i written below one in bean class
public HouseDocumentCharges(Strin g chargeId) {
this.chargeId = chargeId;
}
public boolean equals(Object obj) {
return chargeId.equals( ((HouseDocumentCharges)obj ).chargeId );
}
public int hashCode() {
return chargeId.hashCode();
}
public HouseDocumentCharges(Strin
this.chargeId = chargeId;
}
public boolean equals(Object obj) {
return chargeId.equals( ((HouseDocumentCharges)obj
}
public int hashCode() {
return chargeId.hashCode();
}
Thanks for accepting