I am in stuck with easy things such a loop for and if statement.
I have two different objects:
objectA:
0: id = 1, name = null
1: id = 3, name = null
objectB:
id = 1, name = NameForID1
id = 3, name = NameForID3
My point is to assign names from objectB to objectA by ID value.
I've done double loop + if:
for (int i = 0; i <= objectA.size() - 1; i++){
for(int j = 0; j<=objectB.size() - 1; j++){
if(Objects.requireNonNull(objectA.get(i).getobjectAID()).equals(objectB.get(j).getObjectBID()))
objectA.get(i).setobjectAName(objectB.get(j).getobjectBName());
}
}
And after this I have a list of objectA with last name from objectB array. According to Android Studio Debugger and some... logic... this is logically, but what I am missing and doing wrong?
My bad output:
objectA:
0: id = 1, name = NameForID3
1: id = 3, name = NameForID3
Expected output:
objectA:
0: id = 1, name = NameForID1
1: id = 3, name = NameForID3
UPD
ObjectA class:
class objectA:Serializable {
var objectAID: Int? = null
var objectAName: String? = null
}
ObjectB class:
class objectB {
var objectBName: String? = null
var objectBID: Int? = null
}
UPD. 30.04.2020:
Still getting the same bad output, based on answers below have tried different variations of equals, ==, ===, POJO in Java instead of Kotlin.
UPD: Just wasted your time - when I was trying to found error with your hints, I have malwared my code and started assigning equal IDs to objectA so and names were equal.
I am assuming that the ids are integers:
Try this:
for (int i = 0; i < objectA.size(); i++){
for(int j = 0; j < objectB.size(); j++){
if(objectA.get(i).getobjectAID() == objectB.get(j).getObjectBID()){
//set
objectA.get(i).setobjectAName(objectB.get(j).getobjectBName());
}
}
}
Your POJO must be in java like this:
Object A
public class objectA {
private int objectAID;
private String objectAName;
//generate getters and setters........
}
Object B
public class objectB {
private int objectBID;
private String objectBName;
//generate getters and setters........
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With