我正在尝试重写一些JavaScript逻辑找到的JavaScript函数。 你能帮我把这个JavaScript数组放在一个兼容Java的对象中,这样我就可以遍历所有的elemnet吗?
我需要将它作为Java函数中的硬编码数组。
对我来说,主要问题是它可能包含数组内的数组。
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]
]].....
答案 0 :(得分:1)
创建一个新类来表示复杂对象,因为它们都有label
和value
。由于其中一个属性可以是同一对象的list
,因此您也可以添加它。
类似的东西:
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
}
然后,在数据持有者类中创建一个List<CustomObject> myCustomObjList=new ArrayList<CustomObject>()
,在其中添加元素:
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
依旧......
然后,您可以迭代列表以根据需要获取值。首先检查list
的每个CustomObject的null
属性,如果它不是null
,也会迭代它。
示例:
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();
}
会产生输出:
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
答案 1 :(得分:0)
试试这个:
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;
}
}