Reputation: 33
I'm trying to rewrite some JavaScript logic found JavaScript functions. Could you please help me to put this JavaScript array in some Java compatible object so I could loop over all it's elemnets?
I need this as hardcoded array inside Java function.
The main problem for me is that it could contain arrays inside array.
this.contract = [
["ContractNetworkId",24],
["ContractProvider",8],
["ContractTariff",16],
["ContractSerialNumber",32],
["ContractCustomerInfoBitmap",2,[
["ContractCustomerProfile",6],
["ContractCustomerNumber",32]
]],
["ContractPassengerInfoBitmap",2, [
["ContractPassengerClass",8],
["ContractPassengerTotal",8]
]],
["ContractVehicleClassAllowed",6],
["ContractPaymentPointer",32],
["ContractPayMethod",11],
["ContractServices",16],
["ContractPriceAmount",16],
["ContractPriceUnit",16],
["ContractRestrictionBitmap",7,[
["ContractRestrictStart",11],
["ContractRestrictEnd",11],
["ContractRestrictDay",8],
["ContractRestrictTimeCode",8],
["ContractRestrictCode",8],
["ContractRestrictProduct",16],
["ContractRestrictLocation",16]
]].....
Upvotes: 1
Views: 2144
Reputation: 1583
Make a new class to represent the complex object, since they all have a label
and a value
. Since one of their attributes can be a list
of the same object you can add it as well.
Something like:
public class CustomObject{
private String label;
private int value;
private List<CustomObject> coList;
public CustomObject(String label, int value){
this.label=label;
this.value=value;
}
public CustomObject(String label, int value, List coList){
this.label=label;
this.value=value;
this.coList=coList;
}
//getters and setters for the attributes
}
Then, create a List<CustomObject> myCustomObjList=new ArrayList<CustomObject>()
in you data holder class in which you would add the elements:
myCustomObjList.add(new CustomObject("label",1)); //no list
final CustomObject obj2=new CustomObject("label2",2);
final List<CustomObject> tmpList=new ArrayList<CustomObject>();
tmpList.add(obj2);
myCustomObjList.add(new CustomObject("label",3,tmpList)); //CustomObject with a list
and so on...
You can then iterate the list to get the values as needed. Check the list
attribute of each CustomObject for null
first, if it's not null
, iterate through it as well.
Example:
public static void main(String[] args) {
final List<CustomObject> myCustomObjList = new ArrayList<CustomObject>();
List<CustomObject> tmpList = new ArrayList<CustomObject>();
myCustomObjList.add(new CustomObject("ContractNetworkId", 24));
myCustomObjList.add(new CustomObject("ContractProvider", 8));
myCustomObjList.add(new CustomObject("ContractTariff", 16));
myCustomObjList.add(new CustomObject("ContractSerialNumber", 32));
CustomObject obj1 = new CustomObject("ContractCustomerProfile", 6);
CustomObject obj2 = new CustomObject("ContractCustomerNumber", 32);
tmpList.add(obj1);
tmpList.add(obj2);
myCustomObjList.add(new CustomObject("ContractCustomerInfoBitmap", 2, tmpList));
tmpList=new ArrayList<CustomObject>();
obj1 = new CustomObject("ContractPassengerClass", 8);
obj2 = new CustomObject("ContractPassengerTotal", 8);
tmpList.add(obj1);
tmpList.add(obj2);
myCustomObjList.add(new CustomObject("ContractPassengerInfoBitmap", 2, tmpList));
for (CustomObject parent : myCustomObjList) {
System.out.print(parent.getLabel()+" ; ");
System.out.println(parent.getValue());
if (parent.getCoList() != null) {
for (CustomObject child : parent.getCoList()) {
System.out.print(" "+child.getLabel()+" ");
System.out.println(child.getValue());
}
}
else{
System.out.println(" Parent does not contain children");
}
}
tmpList.clear();
}
would produce the output:
ContractNetworkId ; 24 Parent does not contain children ContractProvider ; 8 Parent does not contain children ContractTariff ; 16 Parent does not contain children ContractSerialNumber ; 32 Parent does not contain children ContractCustomerInfoBitmap ; 2 ContractCustomerProfile 6 ContractCustomerNumber 32 ContractPassengerInfoBitmap ; 2 ContractPassengerClass 8 ContractPassengerTotal 8
Upvotes: 1
Reputation: 578
Try this:
public class Test {
public static void main(String[] args) {
ContractDetail[][] contractDetails = { { new ContractDetail(24, "ContractNetworkId") },
{ new ContractDetail(2, "ContractPassengerInfoBitmap") },
{ new ContractDetail(8, "ContractPassengerClass"), new ContractDetail(8, "ContractPassengerTotal") } };
for (ContractDetail[] contractDetailsAryOuter : contractDetails) {
for (ContractDetail contractDetailInnter : contractDetailsAryOuter) {
System.out.println("Contract Property: " + contractDetailInnter.getId() + ";"
+ contractDetailInnter.getContractProperty());
}
}
}
}
class ContractDetail {
private int id;
private String contractProperty;
public ContractDetail(int id, String contractProperty) {
super();
this.id = id;
this.contractProperty = contractProperty;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContractProperty() {
return contractProperty;
}
public void setContractProperty(String contractProperty) {
this.contractProperty = contractProperty;
}
}
Upvotes: 0