创建3D数组对象

时间:2012-11-17 01:06:03

标签: java object arraylist

我有3个独立的对象,如GSMTable,SMSTable和GPRSTable。 这些包括2个字段,称为srDate和numberOfSR。 现在我需要将它们转换为3D数组对象,因为我需要将它返回到我的jqPlot折线图 这是我的要求:

[
  [gsmTable.srDate][gsmTable.noOfSR],
  [smsTable.srDate][smsTable.noOfSR],
  [gprsTable.srDate][gprsTable.noOfSR]
]

我编写了自己的代码,如下所示,但我无法完成它。

@RequestMapping(value = "/allGridView", method = RequestMethod.GET)
public @ResponseBody
Object[] showLineChart(Map<String, Object> map, HttpServletRequest request,
        HttpServletResponse response) {

    List<GSMTable> gsmList = contactService.fin();
    List<Date>gsmDateList=new ArrayList<Date>();
    List<Integer>gsmCount=new ArrayList<Integer>();
    Object[] gsmArray=new Object[2];
    for (Iterator iterator =gsmList.iterator();iterator.hasNext();) {
        GSMTable gsmTable=(GSMTable)iterator.next();
        gsmDateList.add(gsmTable.getSRDate());
        gsmCount.add(gsmTable.getNumberOfSR());
    }
    gsmArray[0]=gsmDateList.toArray(new Date[gsmDateList.size()]);
    gsmArray[1]=gsmCount.toArray(new Integer[gsmCount.size()]);     
    //=====================================================================================
    List<SMSTable> smsList = contactService.sms();
    List<Date>smsDateList=new ArrayList<Date>();
    List<Integer>smsCount=new ArrayList<Integer>();
    Object[] smsArray=new Object[2];
    for (Iterator iterator =smsList.iterator();iterator.hasNext();) {
        SMSTable smsTable=(SMSTable)iterator.next();
        smsDateList.add(smsTable.getSRDate());
        smsCount.add(smsTable.getNumberOfSR());
    }
    smsArray[0]=smsDateList.toArray(new Date[smsDateList.size()]);
    smsArray[1]=smsCount.toArray(new Integer[smsCount.size()]);
    //=====================================================================================
    List<GPRSTable> gprsList = contactService.gprs();
    List<Date>gprsDateList=new ArrayList<Date>();
    List<Integer>gprsCount=new ArrayList<Integer>();
    Object[] gprsArray=new Object[2];
    for (Iterator iterator =gprsList.iterator();iterator.hasNext();) {
        GPRSTable gprsTable=(GPRSTable)iterator.next();
        gprsDateList.add(gprsTable.getSRDate());
        gprsCount.add(gprsTable.getNumberOfSR());
    }
    gprsArray[0]=gprsDateList.toArray(new Date[gprsDateList.size()]);
    gprsArray[1]=gprsCount.toArray(new Integer[gprsCount.size()]);

    Object[][][] finalArray;
    //after that how to complete it ? ? ? 
    //need to return finalArray as 3D Object Array
    return finalArray;
}

gsmArray:

[[2012-11-05,05],[2012-11-06,08],[2012-11-07,10],[2012-11-08,14],[2012-11-08,09]]

smsArray:

[[2012-11-05,02],[2012-11-06,03],[2012-11-07,04],[2012-11-08,04],[2012-11-08,03]]

gprsArray:

[[2012-11-05,04],[2012-11-06,07],[2012-11-07,08],[2012-11-08,04],[2012-11-08,07]]

我需要将gsmArray,smsArray和gprsArray合并为3D Array 那么我的期望数组应该是:

[[2012-11-05,05],[2012-11-06,08],[2012-11-07,10],[2012-11-08,14],[2012-11-08,09],[2012-11-05,02],[2012-11-06,03],[2012-11-07,04],[2012-11-08,04],[2012-11-08,03],[2012-11-05,04],[2012-11-06,07],[2012-11-07,08],[2012-11-08,04],[2012-11-08,07]]

1 个答案:

答案 0 :(得分:0)

终于得到了解决方案:

    @RequestMapping(value = "/allGridView", method = RequestMethod.GET)
public @ResponseBody
Object[] showLineChart(Map<String, Object> map, HttpServletRequest request,
        HttpServletResponse response) {
    List<MapTable> gsmList = contactService.fin();
    Object[] gsmArray = new Object[gsmList.size()];
    int i = 0;
    for (Iterator iterator = gsmList.iterator(); iterator.hasNext();) {
        MapTable gsmTable = (MapTable) iterator.next();
        Object[] obj = new Object[2];
        obj[0] = gsmTable.getSRDate();
        obj[1] = gsmTable.getNumberOfSR();
        gsmArray[i] = obj;
        i++;
    }

    // =====================================================================================
    List<SMSTable> smsList = contactService.sms();
    Object[] smsArray = new Object[smsList.size()];
    i=0;
    for (Iterator iterator = smsList.iterator(); iterator.hasNext();) {
        SMSTable smsTable = (SMSTable) iterator.next();
        Object[] obj = new Object[2];
        obj[0] = smsTable.getSRDate();
        obj[1] = smsTable.getNumberOfSR();
        smsArray[i] = obj;
        i++;
    }
    // =====================================================================================
    List<GPRSTable> gprsList = contactService.gprs();
    Object[] gprsArray = new Object[gprsList.size()];
    i=0;
    for (Iterator iterator = gprsList.iterator(); iterator.hasNext();) {
        GPRSTable gprsTable = (GPRSTable) iterator.next();
        Object[] obj = new Object[2];
        obj[0] = gprsTable.getSRDate();
        obj[1] = gprsTable.getNumberOfSR();
        gprsArray[i] = obj;
        i++;
    }

    Object[] p=new Object[3];
    p[0]=gsmArray;
    p[1]=smsArray;
    p[2]=gprsArray;
    return p;
}

输出:

[[["2012-10-14",3949681],["2012-10-15",9807773],["2012-10-16",9955313],["2012-10-17",9963638],["2012-10-18",9707045],["2012-10-19",9504716],["2012-10-20",8263961],["2012-10-21",7488501],["2012-10-22",10252551],["2012-10-23",10308166],["2012-10-24",10475042],["2012-10-25",10404402],["2012-10-26",10423749],["2012-10-27",8735703],["2012-10-28",7596690],["2012-10-29",8282781],["2012-10-30",11137772],["2012-10-31",11270296],["2012-11-01",11450240],["2012-11-02",11198160],["2012-11-03",9695753],["2012-11-04",8780354],["2012-11-05",11988513],["2012-11-06",12076273],["2012-11-07",12317203],["2012-11-08",12347978],["2012-11-09",12189239],["2012-11-10",10431736],["2012-11-11",9712163],["2012-11-12",12878093],["2012-11-13",12254237],["2012-11-14",13545518]],[["2012-10-14",2536],["2012-10-15",519490],["2012-10-16",527355],["2012-10-17",532718],["2012-10-18",535088],["2012-10-19",511614],["2012-10-20",466463],["2012-10-21",446447],["2012-10-22",531781],["2012-10-23",539720],["2012-10-24",540633],["2012-10-25",538185],["2012-10-26",615236],["2012-10-27",777230],["2012-10-28",433904],["2012-10-29",484538],["2012-10-30",531702],["2012-10-31",553843],["2012-11-01",560434],["2012-11-02",523517],["2012-11-03",474273],["2012-11-04",436413],["2012-11-05",522902],["2012-11-06",523710],["2012-11-07",532361],["2012-11-08",526496],["2012-11-09",523075],["2012-11-10",480817],["2012-11-11",430512],["2012-11-12",544556],["2012-11-13",783723],["2012-11-14",540902]],[["2012-10-03",6],["2012-10-09",2567],["2012-10-10",2017],["2012-10-11",2662],["2012-10-12",7861],["2012-10-13",21963],["2012-10-14",100925],["2012-10-15",484981],["2012-10-16",421067],["2012-10-17",501338],["2012-10-18",498310],["2012-10-19",514784],["2012-10-20",631011],["2012-10-21",683557],["2012-10-22",512583],["2012-10-23",530384],["2012-10-24",549654],["2012-10-25",569639],["2012-10-26",554665],["2012-10-27",681578],["2012-10-28",750583],["2012-10-29",820117],["2012-10-30",603903],["2012-10-31",592405],["2012-11-01",653473],["2012-11-02",655571],["2012-11-03",706664],["2012-11-04",759478],["2012-11-05",601499],["2012-11-06",623094],["2012-11-07",627868],["2012-11-08",611454],["2012-11-09",706279],["2012-11-10",754463],["2012-11-11",765718],["2012-11-12",642780],["2012-11-13",675966],["2012-11-14",509627]]]