ArrayList Entity属性mystery使用'Integer'对象

时间:2015-03-14 21:03:19

标签: java google-app-engine arraylist

为什么我能够将实体属性设置为ArrayList但不能 ArrayList的?

下面的代码工作正常,但当我将'Long'对象更改为 “整数”对象实体构造正确,但是当我 执行一些代码来编辑实体我得到下面显示的错误 示例代码?

//  Fetch the Datastore Service instance [ds]...
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
//
//  Initialize an Entity [row] for each ‘State Code’...
//
Key stateKey = null;    Entity stateEntity = null;
//  Zero Controls...
Long iZero = new Long(0);
ArrayList<Long> al = new ArrayList<Long>();
for (int i=0; i<4; i++) {   al.add(iZero);  }
//  One Controls
ArrayList<Long> alDebug = new ArrayList<Long>();
Long iOne = new Long(1);    
for (int i=0; i<4; i++) {   alDebug.add(iOne);  }
//   LOOP on 'stateCodes' String Array...
for (String code : stateCodes)      {
    stateKey = KeyFactory.createKey(numbersKind, code);
    stateEntity = new Entity(stateKey);
    if (    code.equals("AZ")   )   {
       stateEntity.setProperty("stateNumbers", alDebug);    }
    else                            {
        stateEntity.setProperty("stateNumbers", al);        }
    ds.put(stateEntity);
}   //  End 'stateCodes' LOOP...
//  Code to test Enity modifications...
//  Get Array List for "AZ"...
Key nStateKey = KeyFactory.createKey(numbersKind, "AZ");
stateEntity = new Entity(nStateKey);
try {
    Entity entityTest = ds.get(nStateKey);
    ArrayList<Long> alAZ =
        (ArrayList<Long>) entityTest.getProperty("stateNumbers");
    Long iTest = alAZ.get(2);    // LINE THAT INVOKES ERROR...
    alAZ.set(2, iTest+new Long(4935));
    ds.put(entityTest);
} catch (EntityNotFoundException e) {
    out.println("Entity does not exist in the datastore...");  }

错误:

java.lang.ClassCastException: java.lang.Long cannot be cast to 
   java.lang.Integer at 
      jFormTKServlets.JFormTKServletsServlet.doGet(
         JFormTKServletsServlet.java:330)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
...

1 个答案:

答案 0 :(得分:0)

编辑:

除了以下事实:

    ArrayList<Long> alAZ =
    (ArrayList<Long>) entityTest.getProperty("stateNumbers");

是否有效,

 Long iTest = (long) a1ZA.get(2); 
如果a1AZ.get(2)返回int

会为您提供所需的结果。 :)